+ -
当前位置:首页 → 问答吧 → SQL 2005 UPDATE 问题

SQL 2005 UPDATE 问题

时间:2011-11-29

来源:互联网

表中有一字段的值存在着纯数字和中文的字符串,如
123
我爱你123、1223
我爱你123我爱你1234我爱你123
我爱你123我爱你
123我爱你
现在需要把这个字段中的前面和后面是中文的去掉,中间有中文或者符号的不管,更新后字段值为
123
123、1223
123我爱你1234我爱你123
123
123
请问SQL更新语句怎么写?

作者: yihuaheren011   发布时间: 2011-11-29

SQL code
create table tb(col nvarchar(30))
insert into tb select '123'
insert into tb select '我爱你123、1223'
insert into tb select '我爱你123我爱你1234我爱你123'
insert into tb select '我爱你123我爱你'
insert into tb select '123我爱你'
go
select substring(col,PATINDEX('%[^吖-做]%',col),len(col)-Patindex('%[^吖-做]%',REVERSE(col))-PATINDEX('%[^吖-做]%',col)+2) from tb
/*
------------------------------
123
123、1223
123我爱你1234我爱你123
123
123

(5 行受影响)

*/

go
drop table tb

作者: qianjin036a   发布时间: 2011-11-29

更新:
SQL code
create table tb(col nvarchar(30))
insert into tb select '123'
insert into tb select '我爱你123、1223'
insert into tb select '我爱你123我爱你1234我爱你123'
insert into tb select '我爱你123我爱你'
insert into tb select '123我爱你'
go
update tb set col=substring(col,PATINDEX('%[^吖-做]%',col),len(col)-Patindex('%[^吖-做]%',REVERSE(col))-PATINDEX('%[^吖-做]%',col)+2)

select * from tb
/*
col
------------------------------
123
123、1223
123我爱你1234我爱你123
123
123

(5 行受影响)

*/

go
drop table tb

作者: qianjin036a   发布时间: 2011-11-29

热门下载

更多