一个字符串
-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- SC(300)03:45- CU(300)04:45- SC(300)05:45- FC(300)08:30-09:40
我要删除以SC起头直到下一个字母为止的部分。
也就是删除SC(300)03:45- 和SC(300)05:45- 两段。在一个字符串中如何找到第一个字母的位置?
-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- SC(300)03:45- CU(300)04:45- SC(300)05:45- FC(300)08:30-09:40
我要删除以SC起头直到下一个字母为止的部分。
也就是删除SC(300)03:45- 和SC(300)05:45- 两段。在一个字符串中如何找到第一个字母的位置?
',
@string2 varchar(10) = 'SC'
select CHARINDEX(@string2,@string1)删除可以用替换:(假设SC*长度为一定,不然在substring中再找下一个空格符。)
select REPLACE(@string1, SUBSTRING(@string1,CHARINDEX(@string2,@string1),14), '')
declare @str varchar(2000)
set @str='-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- SC(300)03:45- CU(300)04:45- SC(300)05:45- FC(300)08:30-09:40'
--select charindex('SC',@str)
--select patindex('%[A-Z]%',right(@str,len(@str)-charindex('SC',@str)-1))
select STUFF(@str,--原字符串
charindex('SC',@str),--起始位置
patindex('%[A-Z]%',right(@str,len(@str)-charindex('SC',@str)-1)),--替换长度
''--用来替换原字符串里指定部分的新字符串
) as newstr/*
newstr
-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- CU(300)04:45- SC(300)05:45- FC(300)08:30-09:40
*/
declare @string1 varchar(1000) = '-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- SC(300)03:45- CU(300)04:45- SC(300)05:45- FC(300)08:30-09:40
',
@string2 varchar(10) = 'SC'while(CHARINDEX(@string2,@string1) > 0)
BEGIN
set @string1 = REPLACE(@string1, SUBSTRING(@string1,CHARINDEX(@string2,@string1),14), '')
END
select @string1
declare @x varchar(6000)select @x='-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- SC(300)03:45- CU(300)04:45- SC(300)05:45- FC(300)08:30-09:40'select @x=replace(@x,substring(@x,charindex('SC',@x),patindex('%[a-zA-Z]%',substring(@x,charindex('SC',@x)+2,6000))),'')select @x=replace(@x,substring(@x,charindex('SC',@x),patindex('%[a-zA-Z]%',substring(@x,charindex('SC',@x)+2,6000))),'')select @x x/*
x
-------------------------------------------------------------------------------------------------------
-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- CU(300)04:45- FC(300)08:30-09:40(1 row(s) affected)
*/
begin
select @x=replace(@x,substring(@x,charindex('SC',@x),patindex('%[a-zA-Z]%',substring(@x,charindex('SC',@x)+2,6000))),'')
endselect @x x/*
x
------------------------------------------------------------------------------------------------------
-BR20:25-22:30 -FS(210)20:25-(240)22:45-(330)00:40- FC(300)02:50- CU(300)04:45- FC(300)08:30-09:40(1 row(s) affected)
*/