问题描述
亲爱的团队,
我有一张桌子。
表名:STDCodeTable
列为按照:
ID int
州varchar(10)
区域varchar(10)
STDCode varchar(10 )
i有一个查询。在STDCode列中,我的值为2345,4567,478248 ......等等。
在所有数字之前我想要0,如02345,它应该是大于4位数。如果STDCode大于4位,那么在STDCode之前加上0。
那么什么是我的更新查询???
i尝试了这个,但它更新了所有STDCOde:
更新STDCodeTable
设置STDCode ='0'+ STDCode
其中STDCode> 2
请帮助。
谢谢
Harshal
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
推荐答案
update STDCodeTable
set STDCode = '0' + STDCode
where len(STDCode) >=4
或
使用REPLICATE
or
use REPLICATE
update STDCodeTable
set STDCode = Replicate('0',1) +StdCode
where len(STDCode)>=4
update table1 set stdcode = '0' + stdcode
where len(stdcode) > 4;
select * from table1;
这篇关于如何在SQL中使用更新查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!