Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Leading 0

Author  Topic 

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2009-12-03 : 07:11:29
Hi Guys

Just a quick question.

How do I place a leading 0 in front of a single digit number?

So

7
8
9
10
11

Becomes

07
08
09
10
11

Thanks

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-12-03 : 07:20:59
Hi try this once,


select right('0'+column,2) from table
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-12-03 : 07:27:26
Hi

Column datatype int means it won't work right

select right('0'+6,2)
select right('0'+'6',2)

-------------------------
R...
Go to Top of Page

creieru
Starting Member

12 Posts

Posted - 2009-12-03 : 07:27:47
select right('0'+@str, 2), where @str is your number converted to varchar
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-12-03 : 07:28:17
SELECT CASE WHEN LEN(id) < 2 THEN '0'+id ELSE id END,LEN(id)
FROM
(
SELECT CAST(4 AS VARCHAR(32)) AS id UNION ALL SELECT CAST(11 AS VARCHAR(32))
)s
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-03 : 07:59:48
quote:
Originally posted by rcr69er

Hi Guys

Just a quick question.

How do I place a leading 0 in front of a single digit number?

So

7
8
9
10
11

Becomes

07
08
09
10
11

Thanks


Where do you want to show formatted data?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2009-12-04 : 02:26:01


SELECT CASE WHEN LEN(id) < 2 THEN REPLACE(STR(id, 2), ' ', '0')
ELSE id END
FROM ...

Go to Top of Page
   

- Advertisement -