Results 1 to 3 of 3

Thread: Padding

  1. #1
    Join Date
    Sep 2003
    Location
    Switzerland
    Posts
    443

    Unanswered: Padding

    Hi,

    I need to rpad integer values in a single string.

    I have say 5 integer values and I am trying to print them in one single line.

    I need to SELECT convert(char,col1)+convert(char,col2) ....etc

    All columns need to have a fixed length. Lets say 8 characters.

    If I do a convert(char(8),col1), then I am getting the integers as left aligned, but I need em right aligned.

    Col1...., Col2.....,
    24, 6,

    Something like above.

    Would appreciate any help.

    thanks,
    Vishi

  2. #2
    Join Date
    Sep 2003
    Location
    Switzerland
    Posts
    443

    Padding

    Figured it out.

    replicate(' ',TOTALLENGTHIWANT-lengthofvariable)+convert(varchar(variable))

  3. #3
    Join Date
    Feb 2005
    Location
    Omaha, NE
    Posts
    83
    Why not just use "str()" builtin function?

    Here is an example you might find useful:

    1> set nocount on
    2> select convert(char(5),12) + "," + convert(char(5),2) + "," + convert(char(5),123)
    3> select str(12,5) + "," + str(2,5) + "," + str(123,5)
    4>
    5> declare @len tinyint
    6> select @len = 5
    7> select convert(varchar(17),str(12,@len) + "," + str(2,@len) + "," + str(123,@len))
    8>
    9>
    10> go

    -----------------
    12 ,2 ,123


    -----------------
    12, 2, 123


    -----------------
    12, 2, 123

Posting Permissions

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