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
 General SQL Server Forums
 New to SQL Server Programming
 cast or convert

Author  Topic 

ben_53
Yak Posting Veteran

67 Posts

Posted - 2011-09-02 : 19:08:55
Hi

I want to view a column as a fixed 12 digit from my_table
col = id (which has thousands of values)

now i want to see all id with a fixed 12 digits

if originally id has a value 99, it should prefix 0000000000 before 99 to make it 12 digits

if it has a value 1234 then it should put 8-0's to make it 12 digit.

please help !

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-02 : 20:21:01
you can do like

SELECT RIGHT('000000000000' + CAST(yourfield AS varchar(12)),12) FROM Table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-09-05 : 05:28:19
or

select replace(str(col,12),' ','0') from your_table

Madhivanan

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

- Advertisement -