Professional Web Applications Themes

Right Justified - Microsoft SQL / MS SQL Server

Probably something you should do at the client application. However, let's say you want 30 spaces... SELECT col = CONVERT(VARCHAR(30), RIGHT(TE(SPACE(1), 30) + 'foo', 30)) UNION ALL SELECT RIGHT(TE(SPACE(1), 30) + 'foobario', 30) I used the convert on the first result set to make the column 30 characters wide in Query yzer. If you leave that out, you can use a variable for the length. In fact, you could create a UDF to handle this pretty easily; CREATE FUNCTION dbo.RightJustify ( str VARCHAR(8000), len INT ) RETURNS VARCHAR(8000) AS BEGIN RETURN RIGHT(TE(SPACE(1), len) + str, len) END GO SELECT col ...

  1. #1

    Default Re: Right Justified

    Probably something you should do at the client application. However, let's
    say you want 30 spaces...


    SELECT col = CONVERT(VARCHAR(30), RIGHT(TE(SPACE(1), 30) + 'foo',
    30))
    UNION ALL
    SELECT RIGHT(TE(SPACE(1), 30) + 'foobario', 30)


    I used the convert on the first result set to make the column 30 characters
    wide in Query yzer. If you leave that out, you can use a variable for
    the length. In fact, you could create a UDF to handle this pretty easily;


    CREATE FUNCTION dbo.RightJustify
    (
    str VARCHAR(8000),
    len INT
    )
    RETURNS VARCHAR(8000)
    AS
    BEGIN
    RETURN RIGHT(TE(SPACE(1), len) + str, len)
    END
    GO

    SELECT col = dbo.RightJustify('foo', 30)
    UNION ALL
    SELECT dbo.RightJustify('foobario', 30)




    "Kevin Munro" <kevin_remove8839421c3amulet.com> wrote in message
    news:3f0c270f$0$45180$65c69314mercury.nildram.net ...
    > Hi, how do you pad out a varchar with leading spaces so that the text is
    > right-justified?
    >
    > Thanks, Kevin.
    >
    >

    Aaron Bertrand - MVP Guest

  2. #2

    Default Re: Right Justified

    Thanks for this Aaron, you're right - I should be doing this at the front
    end. What I'm really doing is padding out some numbers with leading zero's
    which can be displayed in a hierarchy and I don't want to recompile my app
    yet to do it. Don't know why I said spaces, but I just convert your
    function as it's still a varchar.

    Many thanks, Kevin.

    "Aaron Bertrand - MVP" <aaronTRASHaspfaq.com> wrote in message
    news:%23e9dbeiRDHA.2332TK2MSFTNGP10.phx.gbl...
    > Probably something you should do at the client application. However,
    let's
    > say you want 30 spaces...
    >
    >
    > SELECT col = CONVERT(VARCHAR(30), RIGHT(TE(SPACE(1), 30) + 'foo',
    > 30))
    > UNION ALL
    > SELECT RIGHT(TE(SPACE(1), 30) + 'foobario', 30)
    >
    >
    > I used the convert on the first result set to make the column 30
    characters
    > wide in Query yzer. If you leave that out, you can use a variable for
    > the length. In fact, you could create a UDF to handle this pretty easily;
    >
    >
    > CREATE FUNCTION dbo.RightJustify
    > (
    > str VARCHAR(8000),
    > len INT
    > )
    > RETURNS VARCHAR(8000)
    > AS
    > BEGIN
    > RETURN RIGHT(TE(SPACE(1), len) + str, len)
    > END
    > GO
    >
    > SELECT col = dbo.RightJustify('foo', 30)
    > UNION ALL
    > SELECT dbo.RightJustify('foobario', 30)
    >
    >
    >
    >
    > "Kevin Munro" <kevin_remove8839421c3amulet.com> wrote in message
    > news:3f0c270f$0$45180$65c69314mercury.nildram.net ...
    > > Hi, how do you pad out a varchar with leading spaces so that the text is
    > > right-justified?
    > >
    > > Thanks, Kevin.
    > >
    > >
    >
    >

    Kevin Munro Guest

  3. #3

    Default Re: Right Justified

    SELECT TE(SPACE(1), 30)
    vs
    SELECT SPACE(30)
    ?

    Bye,
    Delbert Glass


    Delbert Glass Guest

  4. #4

    Default Re: Right Justified

    Yep, I always forget about that one, so sorry...





    "Delbert Glass" <delbertnoincoming.com> wrote in message
    news:uvzrojmRDHA.2676TK2MSFTNGP10.phx.gbl...
    > SELECT TE(SPACE(1), 30)
    > vs
    > SELECT SPACE(30)
    > ?
    >
    > Bye,
    > Delbert Glass
    >
    >

    Aaron Bertrand [MVP] Guest

Similar Threads

  1. Justified text flow - IDCS2
    By Kain_Glover@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 10
    Last Post: April 30th, 04:47 PM
  2. IDCS Hyphenation for justified columns s
    By arcadia_chong@adobeforums.com in forum Adobe Indesign Windows
    Replies: 3
    Last Post: August 27th, 11:11 AM
  3. Right justified tabs don't line up.
    By kevan_j_atteberry@adobeforums.com in forum Adobe Illustrator Macintosh
    Replies: 3
    Last Post: May 12th, 05:14 PM
  4. Fully justified text in Flash MX. How?
    By Seddon in forum Macromedia Flash Sitedesign
    Replies: 1
    Last Post: February 16th, 02:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139