100分求一简单SQL数据库记录处理语句

来源:百度知道 编辑:UC知道 时间:2024/05/21 14:08:10
表userinfo中有一字段tel,是字符型。 现在我要让表中的所有tel记录,只要是aaa开头的都去掉aaa,比如:aaa12312变成12312, aaa32变成32. 请教replace语句怎么写,在线等~~
还有种情况,比如是 12aaa212 这种处理下来需要是 12aaa212 ,也就是除了开头是aaa的要处理,其它的都不变

update userinfo
set tel = substring(tel, 4, len(tel))
where substring(tel, 1, 3) = 'aaa'
直接replace的话会把字段中其它为aaa的也去掉而不光是开头3个

先看看大概情况:
select * from userinfo where tel like 'aaa%'

然后开始更新:
update userinfo set tel=replace(tel,'aaa','') where tel like 'aaa%'
其实也可以
update userinfo set tel=right(tel,len(tel)-3) where tel like 'aaa%'

再看看最后结果:
select * from userinfo where tel like 'aaa%'
应该记录为0了

真的很简单,这也要100分,太有钱了..

update userinfo set tel=replace(tel,'aaa','') where tel like 'aaa%'

update userinfo set tel=replace(cast(tel as 字段类型),'aaa','')
where tel like 'aaa%'

mark

直接将aaa*替换成*不行吗?