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