Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.

  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(REPLICATE(SPACE(1), 30) + 'foo',
    30))
    UNION ALL
    SELECT RIGHT(REPLICATE(SPACE(1), 30) + 'foobario', 30)


    I used the convert on the first result set to make the column 30 characters
    wide in Query Analyzer. 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(REPLICATE(SPACE(1), @len) + @str, @len)
    END
    GO

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




    "Kevin Munro" <kevin_remove8839421@c3amulet.com> wrote in message
    news:3f0c270f$0$45180$65c69314@mercury.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. Similar Questions and Discussions

    1. Justified text flow - IDCS2
      I had a good search but couldn't come up with any info. A couple of years ago I went to the Adobe roadshow (debut of CS1), and they talked about...
    2. IDCS Hyphenation for justified columns sucks
      Sucks is a strong word I know. I have been using InDesign for 18 months or so — as a Quark refugee, I'm really enjoying it. I wouldn't go back —...
    3. Right justified tabs don't line up.
      Aaaargh. this is driving me bananas . . . I have about 12 or 14 lines (think of a price list) with left justified type, then a leader and right-just...
    4. Fully justified text in Flash MX. How?
      What's the best way to display fully justified text in a flash movie? The movie will periodically need to be updated and each "page" on the movie...
  3. #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" <aaron@TRASHaspfaq.com> wrote in message
    news:%23e9dbeiRDHA.2332@TK2MSFTNGP10.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(REPLICATE(SPACE(1), 30) + 'foo',
    > 30))
    > UNION ALL
    > SELECT RIGHT(REPLICATE(SPACE(1), 30) + 'foobario', 30)
    >
    >
    > I used the convert on the first result set to make the column 30
    characters
    > wide in Query Analyzer. 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(REPLICATE(SPACE(1), @len) + @str, @len)
    > END
    > GO
    >
    > SELECT col = dbo.RightJustify('foo', 30)
    > UNION ALL
    > SELECT dbo.RightJustify('foobario', 30)
    >
    >
    >
    >
    > "Kevin Munro" <kevin_remove8839421@c3amulet.com> wrote in message
    > news:3f0c270f$0$45180$65c69314@mercury.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

  4. #3

    Default Re: Right Justified

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

    Bye,
    Delbert Glass


    Delbert Glass Guest

  5. #4

    Default Re: Right Justified

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





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

    Aaron Bertrand [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may 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