Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi All,I have a table called Table1 with a varchar field ID. In my select I want to retrieve all the records but want to put leading zero's in the ID where the length is less than 5 characters. So for example following are some records;ID 123412123457888Now my select should retrieve the following.ID_new 0123400012123450000700888How to do that in my SELECT query?Thanks a million for the quick help.Zee
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2009-09-02 : 19:24:51
select right(' ' + id, 5) from table1hmm, a lot of questions like this lately...Be One with the OptimizerTG
zeeshan13
Constraint Violating Yak Guru
347 Posts
Posted - 2009-09-02 : 19:29:11
TG you mean select right('0' + id, 5) from table1 . Right?
zeeshan13
Constraint Violating Yak Guru
347 Posts
Posted - 2009-09-02 : 19:34:34
What you suggested is wrong. I think it should be the following.SELECT right('00000' + id, 5) from table1Can someone confirm please?Thanks....
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2009-09-02 : 19:44:40
duh, yeah I meant to put zeros not spaces and since I didn't use [ code ] tags the extra spaces got nixed.That looks good to me zee.Be One with the OptimizerTG