如何在SQL中使用更新查询。 [英] how to use update query in SQL.

查看:134
本文介绍了如何在SQL中使用更新查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的团队,



我有一张桌子。

表名: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


如果你想将'0'添加到长度超过4位的stdcode值,那么试试这个:

If you want to add '0' to stdcode value that is longer than 4 digits then try this:
update table1 set stdcode = '0' + stdcode
where len(stdcode) > 4;
select * from table1;


Google给了我这个

格式化数字以添加前导零 - SQL Server [ ^ ]

如何填充前导零的数字类型列? [ ^ ]
Google gave me this
Formatting number to add leading zeros - SQL Server[^]
How to pad a numeric type column with leading zeros?[^]


这篇关于如何在SQL中使用更新查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆