Adding leading zero's (padding out)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lornab
    New Member
    • Sep 2009
    • 7

    Adding leading zero's (padding out)

    Good Afternoon

    I need a column to show in a report as 10 digits long by adding zero's to the left of the result. My select statement is like this:

    SELECT
    a.client,
    a.apar_id

    FROM agltransact a
    where a.client = 'SD'
    and a.account NOT IN ('9600','9621')
    and a.dim_2 <> ''
    and a.voucher_type = 'AP'

    my result is:

    client apar_id
    SD 2097
    SD 2013
    SD 20026
    SD 2531
    SD 2472
    SD 2085

    What I need is the apar_id column to be 10 digits long, preceeded with leading zero's. The results could have as little as 3, or as many as 7 digits.Can anyone help me out with the sql to show this? I am using ms sql server. The column I believe is a varchar.

    Many Thanks

    Lorna
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Convert it, then pad it...

    Code:
    right(replicate('0',10) + ltrim(cast(apar_id as varchar(10))),10)
    Happy Coding!!!


    --- CK

    Comment

    Working...