0

Possible Duplicate:
Most efficient T-SQL way to pad a varchar on the left to a certain length?

I have the customer table where the customerID is of length:10 but there are few customer where the length is 3 or 5

ex: 
3445
34
789
7800

but the output should be like the following, I need to prefix zero here if length is less than 10

0000003445
0000000034
0000000789
0000007800

Script to get this data is:

select customerID from customer
halfer
  • 19,824
  • 17
  • 99
  • 186
happysmile
  • 7,537
  • 36
  • 105
  • 181

3 Answers3

2

Try the following code:

Since you are using sql server 2008, you could use replicate function to add zeros to the value..

Select ltrim(right(replicate(0,10) + <column>,10)) 
from your_table
Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58
1

You can use in this way...

SELECT RIGHT('0000000000' + RTRIM('3445'), 10)

In your case,

SELECT RIGHT('0000000000' + RTRIM(customerID), 10) AS New_CustomerID
FROM Customer
Abdul Ahad
  • 2,187
  • 4
  • 24
  • 39
0

Try this

SELECT RIGHT('0000000000'+`customerID`, 10) FROM `customer`
Bunjerd Sparrow
  • 88
  • 1
  • 10