• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1535
  • Last Modified:

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.
0
Carl2006
Asked:
Carl2006
  • 2
2 Solutions
 
Guy Hengel [angelIII / a3]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 PatelCommented:
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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now