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 2012 Forums
 Transact-SQL (2012)
 How to place 0 in front of value

Author  Topic 

keesvo
Starting Member

10 Posts

Posted - 2015-02-16 : 04:13:29
Hello,

I need a query that gives a result with a 0 in front of values in a certain column (which is a Varchar30 column).

For Example:
12 - must be: 000012
112 - must be: 000112
1112 - must be: 001112

Can someone help me to figure this one out.
Thank you in advance.

Kees

stepson
Aged Yak Warrior

545 Posts

Posted - 2015-02-16 : 04:31:47
[code]
WITH cteSample
AS
(SELECT 12 iValue UNION ALL
SELECT 112 UNION ALL
SELECT 1112)


SELECT iValue , REPLACE(STR(iValue,6),' ','0') as vcValue
FROM cteSample
[/code]



sabinWeb MCP
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2015-02-16 : 04:34:16
and in SQL 2012 , you can use FORMAT :
FORMAT(iValue ,'000000')


sabinWeb MCP
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2015-02-16 : 04:35:51
also this can be used:
RIGHT('000000' + CONVERT(VARCHAR(30), iValue), 6)


sabinWeb MCP
Go to Top of Page

keesvo
Starting Member

10 Posts

Posted - 2015-02-16 : 04:59:50
The first solution did the trick for me !
thanks a lot !
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2015-02-16 : 05:00:30
Welcome!


sabinWeb MCP
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2015-02-16 : 05:42:47
Where are you showing the result set? If it is a front end application, do this formation there

Madhivanan

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

- Advertisement -