仅知道起始字符串时删除整行

时间:2017-09-15 14:47:11

标签: sql sql-server

我正在尝试删除其中包含特定子字符串Signed By:的任何行。

所以原始字符串看起来像:
information on line 1 information on line 2 Signed By: John Smith information on an additional line Signed By: Jane Doe

删除线后 information on line 1 information on line 2 information on an additional line

我遇到的问题是,当我可以轻松替换Signed By:时,我需要删除它后面的名称,这可能会有不同数量的字符。

修改
为了使问题更清楚,所有这些都包含在数据库中的单个字段中。如果我要说的话,我会得到整个原始字符串 SELECT TOP 1 NoteValue FROM Notes

3 个答案:

答案 0 :(得分:0)

您可以使用split函数将字符串拆分为基于char(13)或char(10)的行。然后,将其与xml一起填充。

<强> ONLINE DEMO

declare @table table (strr varchar(4000))
insert into @table
values
('information on line 1
Signed By: John Smith
information on line 2
Signed By: John Smith
information on an additional line
Signed By: Jane Doe')

select
    stuff(( SELECT ' ' + x.Item 
            from @table
            cross apply DelimitedSplit8K(strr,char(13)) x
            where Item not like '%Signed By:%'
            FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

<强>返回

information on line 1  
information on line 2  
information on an additional line

HERE IS THE SPLITTER I USE

USE [Test01]
GO

/****** Object:  UserDefinedFunction [dbo].[DelimitedSplit8K]    Script Date: 09/15/2017 9:59:16 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[DelimitedSplit8K] (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!

RETURNS TABLE WITH SCHEMABINDING AS
RETURN

/* "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
enough to cover VARCHAR(8000)*/

  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;
GO

答案 1 :(得分:0)

你可以尝试

SELECT TOP 1 
   CASE WHEN PATINDEX(NoteValue,'%Signed by%')>0
        THEN  LEFT(NoteValue, PATINDEX(NoteValue,'%Signed by%')-1) 
        ELSE NoteValue 
   END FROM Notes

答案 2 :(得分:0)

使用TSQL的另一种解决方案

DECLARE @document varchar(max);  
SET @document = 'information on line 1
information on line 2
Signed By: John Smith
information on an additional line
Signed By: Jane Doe';  

DECLARE @index int =0;
DECLARE @newLineindex int =0;
DECLARE @ReplaceText varchar(100) = 'Signed'
SELECT CHARINDEX(@ReplaceText, @document, @index)

WHILE ((SELECT CHARINDEX(@ReplaceText, @document, @index)) > 0)
BEGIN
    SELECT @index = CHARINDEX(@ReplaceText, @document, @index);  
    SELECT @newLineindex = CHARINDEX(CHAR(13), @document, @index); 
    IF(@newLineindex >@index)
    BEGIN
        SET @document = REPLACE(@document, SUBSTRING ( @document ,@index , (@newLineindex - @index)), '')
    END
    ELSE
    BEGIN
        SET @document = REPLACE(@document, SUBSTRING ( @document ,@index , ((len(@document) - @index) +1)), '')
    END
    SET @index =0
END
SELECT @document