第二次尝试更新:
这次我创建了以下 UDF,它试图找出用户名并基于它替换模式。由于它是 UDF,因此它适用于模式的多次出现。我认为从速度上来说这不是一个好的解决方案。
GO
CREATE FUNCTION dbo.test(@input varchar(2000))
RETRUNS varchar(2000)
AS
BEGIN
DECLARE
@start_pos int = 0
,@end_pos1 int = 0
,@r varchar(2000) = ''
,@r1 varchar(2000) = ''
,@to_del varchar(2000) = ''
,@user_name varchar(2000) = ''
set @r = @input
WHILE patindex('%record undeleted by_(%_@_[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9])%', @r) > 0
BEGIN
set @start_pos = 0; set @r1 = ''; set @user_name = ''; set @end_pos1 = 0 set @to_del = '';
set @start_pos = len('record undeleted by_(') + patindex('%record undeleted by_(%_@_[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9])%', @r)
set @r1 = substring(@r, @start_pos, len(@r))
set @user_name = ltrim(rtrim(substring(@r1, 1, patindex('%_@_[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9])%', @r1))))
set @end_pos1 = LEN(@user_name) + 15
set @to_del = 'record undeleted by ('+ ltrim(rtrim(substring(@r1, 1, @end_pos1)))
set @r = replace(@r, @to_del, '')
END
RETURN @r
END
GO
以下是使用 UDF 的测试表和 SELECT 语句的代码。
declare @t table
(
id int identity(1,1)
,dscr varchar(2000)
)
insert into @t(dscr)
select 'Credit received 08/11/2019 record undeleted by (@ahmedhuq @ 08/11/2019)' union
select 'Request removal of ship to address record undeleted by (group1\user1 @ 20/09/2019)' union
select 'Request removal of ship to address record undeleted by (group1\user1 @ 20/09/2019) and the quick brown fox' union
select 'Request removal of ship to address record undeleted by (dir1\user2 @ 20/09/2019) and Credit received 08/11/2019 record undeleted by (@ahmedhuq @ 08/11/2019)' union
select 'Request removal of ship to address record undeleted'
select * , dbo.test(dscr)
from @t where dscr like '%record undeleted by_(%_@_[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9])%'
结果如下:
忽略尝试 1:
下面的脚本不能用作工作解决方案。如果模式只出现一次,它就可以正常工作。对于多次出现的模式,数据会损坏!
declare @t table
(
id int identity(1,1)
,dscr varchar(2000)
)
insert into @t(dscr)
select 'Credit received 08/11/2019 record undeleted by (@ahmedhuq @ 08/11/2019)' union
select 'Request removal of ship to address record undeleted by (group1\user1 @ 20/09/2019)' union
select 'Request removal of ship to address record undeleted by (group1\user1 @ 20/09/2019) and the quick brown fox' union
select 'Request removal of ship to address record undeleted by (dir1\user2 @ 20/09/2019) and Credit received 08/11/2019 record undeleted by (@ahmedhuq @ 08/11/2019)' union
select 'Request removal of ship to address record undeleted'
select * from @t
where dscr like '%record undeleted by_(%_@___/__/____)%'
;with a as
(
select *
,patindex( '%record undeleted by_(%_@___/__/____)%', dscr) as start_pos
,patindex( reverse('%record undeleted by_(%_@___/__/____)%'), reverse(dscr)) as end_pos_reverse
,len(dscr) - patindex( reverse('%record undeleted by_(%_@___/__/____)%'), reverse(dscr)) + 2 as end_pos
from
@t
)
,a1 as
(
select *
,end_pos - start_pos as start_end_len
from
a
)
,a2 as
(
select
*,substring(dscr, start_pos, start_end_len) replace_str
from
a1
)
select
*
,replace(dscr, replace_str, '') as result1
from
a2