Solved

convert 10 to 010 in t-sql

Posted on 2007-12-03
6
1,480 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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