Solved

convert 10 to 010 in t-sql

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

17 Experts available now in Live!

Get 1:1 Help Now