如何删除某列的行值中包含两个 c 字符的所有行------patindex函数的使用

来源:互联网 发布:2016年度网络十大词汇 编辑:程序博客网 时间:2024/06/07 13:44

PATINDEX('%pattern%',expression), 返回指定表达式中第一次出现的起始位置;如果不存在,则返回零。

 

新建 测试表

create table a(zifu varchar(10) null)

insert table(zifu)

select 'ac'

union all

select 'b'

union all

select 'xccb'

union all

select 'abcauc90c'

union all

select 'c'

select * from a

/*结果*/

zifu
----------
ac
b
xccb
abcauc90c
c

(5 行受影响)

 

delete from a where patindex('%c%c%',zifu)>0

/*执行后结果*/
(2 行受影响)

/*查询删除后的结果*/

select * from a

zifu
----------
ac
b
c

(3 行受影响)

 

另一种解法:

思路:使用replace 函数,先将c字符替换为空,再用len函数对比替换前后的长度,如果再者相减差值大于等于2则删除这部分列。

delete  from a where (len(zifu)-len(replace(zifu,'c','')) )>=2


 

 

 

 

 

原创粉丝点击