By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,419 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,419 IT Pros & Developers. It's quick & easy.

Adding leading zero's (padding out)

P: 7
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:


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

Oct 5 '09 #1
Share this Question
Share on Google+
1 Reply

Expert 2.5K+
P: 2,878
Convert it, then pad it...

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

--- CK
Oct 7 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.