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)
 update query

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-10 : 08:47:07
how can i update all records that are only 2 digits I want to add a 0 in front of them (this is a varchar field)
so 123 will stay 123 but 12 will turn to 012?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-10 : 08:49:35
[code]UPDATE YourTable
SET yourcol=RIGHT('0' + yourcol,3)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-10 : 08:50:26
or

UPDATE YourTable
SET yourcol='0' + yourcol
WHERE LEN(yourcol)=2


------------------------------------------------------------------------------------------------------
SQL Server MVP
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-10 : 09:25:00
quote:
Originally posted by esthera

how can i update all records that are only 2 digits I want to add a 0 in front of them (this is a varchar field)
so 123 will stay 123 but 12 will turn to 012?




If you use front end application, do formation there

Madhivanan

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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-10 : 09:26:05
quote:
Originally posted by visakh16

or

UPDATE YourTable
SET yourcol='0' + yourcol
WHERE LEN(yourcol)=2


------------------------------------------------------------------------------------------------------
SQL Server MVP



or


UPDATE YourTable
SET yourcol=replace(str(yourcol,3),' ','0')



Madhivanan

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

- Advertisement -