如果前4个与搜索匹配,我期待删除36个字符。
示例:
搜索:ABCD
输入:
CDBAhdjashbjhjk833h12kj3h21jk3h21123ABCD32872879dhc37cdkx76lx743hdadasd7CDBAhdjashbjhjk833h12kj3h21jk3h21123
结果:
CDBAhdjashbjhjk833h12kj3h21jk3h21123CDBAhdjashbjhjk833h12kj3h21jk3h21123
正如您所看到的,"ABCD32872879dhc37cdkx76lx743hdadasd"
已从行中删除。
如何在sql上实现这一点?
我尝试这种方法,因为我用PHP做了一切,但是一些提取的数据高于字符串的默认限制,即4096。
谢谢
PD:使用MSSQL。
答案 0 :(得分:1)
DECLARE @var NVARCHAR (MAX) = 'CDBAhdjashbjhjk833h12kj3h21jk3h21ABCD32872879dhc37cdkx76lx743hdadasd7CDBAhdjashbjhjk833h12kj3h21jk3h21'
DECLARE @Search VARCHAR(MAX) = 'ABCD'
DECLARE @test varchar(max)
DECLARE @test1 varchar(max)
DECLARE @index int, @len int
set @len = LEN(@var)
print @len
set @index = CHARINDEX(@Search,@Var)
set @test = substring(@var,0,@index)
set @test1 = substring(@var,@index+36,@len)
select @test+@test1
这应该有帮助...对于命名惯例
抱歉答案 1 :(得分:0)
这是普通的标准SQL:
CASE
WHEN POSITION('ABCD' IN col) > 0
THEN SUBSTRING(col FROM 1 FOR POSITION('ABCD' IN col) - 1)
|| SUBSTRING(col FROM POSITION('ABCD' IN col) + 36)
ELSE col
END
添加SQL Server标记后编辑:
CASE
WHEN CHARINDEX('ABCD', col) > 0
THEN SUBSTRING(col, 1, CHARINDEX('ABCD', col) - 1)
+ SUBSTRING(col, CHARINDEX('ABCD', col) + 36, 5000)
ELSE col
END