Right Justifiy in Fixed length field.

  • After my last post (MUCK tables)... please be gentle.  (Yes, I owe her dinner!)

    I am trying to create a fixed length field in an output file using a query.  So I am CAST(<column name> as CHAR(10)).  Well, I would like the data to be right justified.  The only solution that I can come up with is using CASE WHEN and measure the column with LEN and then concatenate the required number of spaces in front of the data.  Anyone know of a better method? 

     

    Thanks

     

    Steve

  • declare @Foo varchar(5),

        @bar char(10)

    set @Foo = 'abc'

    set @bar = replicate(' ',10 - len(@Foo)) + @Foo

    select @bar

     

  • Sorry but this is a presentation issue and it should be taken care of application side.

  • Well it really is not a presentation issue since the data is being exported into a file and the client wants it right justified.  - But I get your point

  • Alright... can't go against what they want .

  • Clients are gods - $$ flow from them... but life would be easier... I guess I won't go there.

  • Just adding my two cents...

    Use STR(n,l,d) for numerics on this...

    n = number to convert

    l = final length of all characters (defaults to 10)

    d = number of decimal places (defaults to 0)

    If they need to be zero filled...

    REPLACE(STR(n,l,d),' ','0')

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply