Solved

convert 10 to 010 in t-sql

Posted on 2007-12-03
6
1,472 Views
Last Modified: 2013-11-16
Hello,
How can i convert 10 to 010 in sql?

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

thanks in advance.
0
Comment
Question by:Carl2006
  • 2
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 63 total points
ID: 20396226
select right('000' + cast(yourfield as varchar(3)), 3)
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20396253
Are these values in a column, if yes then use replicate function
0
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 62 total points
ID: 20396273
Like this, replace '1' with your column name if you have.
SELECT REPLICATE('0', 3 - DATALENGTH('1')) + '1'
0
 

Expert Comment

by:alberttnt
ID: 21476844
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now