Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Team,

I have one table .
Table Name: STDCodeTable
column are as follow:
ID int
State varchar(10)
Area varchar(10)
STDCode varchar(10)

i have one query .In STDCode Column, i have the value like 2345,4567,478248 ... and so on.
I want 0 before all digits like 02345 and it should be for greater than 4 digit.if the STDCode is greater than 4 digit then add 0 before STDCode.
so what will be my update query???
i have tried this but it updating all the STDCOde :
update STDCodeTable
set STDCode = '0' + STDCode
where STDCode >2

Kindly help.

Thanks
Harshal
Posted

SQL
update STDCodeTable
set STDCode = '0' + STDCode
where len(STDCode) >=4


or

use REPLICATE


SQL
update STDCodeTable
set STDCode = Replicate('0',1) +StdCode
where len(STDCode)>=4
 
Share this answer
 
v3
Comments
R Harshal 21-Feb-14 8:37am    
Thanks for your reply.
Thank you so much
If you want to add '0' to stdcode value that is longer than 4 digits then try this:
SQL
update table1 set stdcode = '0' + stdcode
where len(stdcode) > 4;
select * from table1;
 
Share this answer
 
Comments
R Harshal 21-Feb-14 8:35am    
Thanks Brother .I realised my mistake..
Thank you so much
 
Share this answer
 
Comments
R Harshal 21-Feb-14 7:45am    
thanks for your quick reply but it does not work for me.
My requirement is i want to place 0 for 4 digit not for all.
Please Guide me..
Thanks
Harshal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900