convert 10 to 010 in t-sql

Hello,
How can i convert 10 to 010 in sql?

Examples:
1 -> 001
100 --> 100
0 --> 000

thanks in advance.
Carl2006Asked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
select right('000' + cast(yourfield as varchar(3)), 3)
0
 
Ashish PatelCommented:
Are these values in a column, if yes then use replicate function
0
 
Ashish PatelConnect With a Mentor Commented:
Like this, replace '1' with your column name if you have.
SELECT REPLICATE('0', 3 - DATALENGTH('1')) + '1'
0
 
alberttntCommented:
Hi Carl2006,

Let's assume your data value is integer and the variable name is @DataValue

Use the following query to get the result you want:

SELECT REPLICATE('0', 3 - LEN(@DataValue)) + CAST(@DataValue AS VARCHAR(MAX))

Hope my post can help you.

B/Rgds,
alberttnt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.