Connecting Tech Pros Worldwide Help | Site Map

Adding leading zero's (padding out)

Newbie
 
Join Date: Sep 2009
Posts: 4
#1: Oct 5 '09
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's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Oct 7 '09

re: Adding leading zero's (padding out)


Convert it, then pad it...

Expand|Select|Wrap|Line Numbers
  1.  
  2. right(replicate('0',10) + ltrim(cast(apar_id as varchar(10))),10)
  3.  
  4.  
Happy Coding!!!


--- CK
Reply


Similar Microsoft SQL Server bytes