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)
 Adding Leading 00's

Author  Topic 

rwaldron
Posting Yak Master

131 Posts

Posted - 2008-03-31 : 10:13:28
Hiya all,
I have a linked table between SQL and BTrieve.
I have a column that returns 1 ,2 ,10 etc.
In Btrieve the datatype is numeric and 3 digits long.

But in SQL I want 001 , 002 ,010.
Anyone got some code to programatically add 00 if 1,2,3
or add o if 10,11 or add none if 100,101

I also tried casting as in CAST(table.ID AS NUMERIC(3,0)) as Del_ID,
but SQL still returns 1,2,10 and not 001,002,010

nr
SQLTeam MVY

12543 Posts

Posted - 2008-03-31 : 10:14:46
select right('000' + convert(varchar(3),fld),3)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-02 : 06:39:06
Also you can use

select right('00' + convert(varchar(3),fld),3)

Provided fld is not empty

Madhivanan

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

rwaldron
Posting Yak Master

131 Posts

Posted - 2008-04-02 : 07:43:48
Thx guys
select right('000' + convert(varchar(3),fld),3)
Did the trick

Ray..
Go to Top of Page
   

- Advertisement -