How to get text between two words/characters

前端 未结 4 772
不知归路
不知归路 2021-01-27 01:15

How can I get some text between two known words?

Sub-select example:

(SELECT TOP(1)Note
FROM Clients
WHERE (ID=@ID) AND (Note IS NOT NULL) AND (Note NOT         


        
相关标签:
4条回答
  • 2021-01-27 01:33
    CREATE FUNCTION [dbo].[fnSplitString] 
    ( 
        @string NVARCHAR(MAX),
        @id int , ,
        @sword varchar(30) ,
        @delimiter varchar(30) 
    ) 
    RETURNS @output TABLE(splitdata NVARCHAR(MAX) , id int
    ) 
    BEGIN 
        DECLARE @start INT, @end INT 
        declare @start1 varchar(100)
        set @start1 = @sword
    
        declare @end1 varchar(100)
        set @end1 =  @delimiter
        SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) + Len(@delimiter) - 1
        WHILE @start < LEN(@string) + 1 BEGIN 
            IF @end = 0  
                SET @end = LEN(@string) + 1
    
            INSERT INTO @output (splitdata , id)  
            VALUES(substring( LEFT((SUBSTRING(@string, @start, @end - @start)), charindex(@end1, (SUBSTRING(@string, @start, @end - @start)), CHARINDEX(@start1, (SUBSTRING(@string, @start, @end - @start))))-1), CHARINDEX(@start1, (SUBSTRING(@string, @start, @end - @start))) + len(@start1), LEN((SUBSTRING(@string, @start, @end - @start))))  ,@id ) 
    
            SET @start = @end + 1 
            SET @end = CHARINDEX(@delimiter, @string, @start)+ Len(@delimiter) - 1
             END 
        RETURN 
    END
    
    Select note , fnSplitString (note , id ,'startword', 'endword') from clients 
    
    0 讨论(0)
  • 2021-01-27 01:35

    Guys I found my own solution:

    DECLARE @S VARCHAR(MAX) = 'Blablabla bla bla blabla bla bla bla (21.08.2015) "some text" word bla bla Blablabla bla bla blabla bla bla bla (25.08.2015) "another text" word bla bla Blablabla bla bla blabla bla bla bla (28.08.2015) "TEXT I NEED TO GET" word bla bla blabla bla bla.'

    SELECT LEFT(REVERSE(LEFT(REVERSE(@S), CHARINDEX(')', REVERSE(@S)) - 2))+'word', CHARINDEX('word',REVERSE(LEFT(REVERSE(@S), CHARINDEX(')', REVERSE(@S)) - 2))+'word')-1) AS respond

    This returns exactly:

    "TEXT I NEED TO GET"

    Thank you all for leading me to this point.

    0 讨论(0)
  • 2021-01-27 01:40

    Maybe something like this:

    You try to find the last occurence. This is the first in a reverted string...

    --This is a mockup of your table to hold your strings:

    DECLARE @tbl TABLE(YourLine VARCHAR(100));
    INSERT INTO @tbl VALUES
     ('Blablabla bla bla blabla bla bla bla (21.08.2015) "some text" word bla bla')
    ,('Blablabla bla bla blabla bla bla bla (25.08.2015) "another text" word bla bla') 
    ,('Blablabla bla bla blabla bla bla bla (28.08.2015) "TEXT I NEED TO GET" word bla bla blabla bla bla.');
    

    --Here I specify the word you are searching for

    DECLARE @SearchWord VARCHAR(100)='word';
    

    --This is the query

    SELECT REVERSE(C.CutOut) AS YourSnippet
    FROM @tbl AS tbl
    

    --CROSS APPLY works row-wise: Take the current line and revert it!

    CROSS APPLY(SELECT REVERSE(tbl.YourLine) AS RevLine) AS A
    

    --Find the first occurence of ) and the first occurence of the reverted searchword!

    CROSS APPLY(SELECT CHARINDEX(')',A.RevLine) AS PosParanthesis
                      ,CHARINDEX(REVERSE(@SearchWord),A.RevLine) AS PosWord) AS B 
    

    --Cut the string at the position found to the length calculated

    CROSS APPLY(SELECT SUBSTRING(A.RevLine,B.PosWord + LEN(@SearchWord)+1,B.PosParanthesis-B.PosWord-LEN(@SearchWord)-1) AS CutOut) AS C
    

    The result of the las CROSS APPLY is your target string, but reverse. Therefore a last reverse in the main SELECT...

    0 讨论(0)
  • 2021-01-27 01:46
    declare @start varchar(100)
    set @start = 'startword'
    
    declare @end varchar(100)
    set @end = 'endword'
    
    SELECT TOP(1) Note , substring( LEFT(note, charindex(@end, note, CHARINDEX(@start, note))-1), CHARINDEX(@start, note) + len(@start), LEN(note))   result
    FROM Clients
    WHERE (ID=@ID) AND (Note IS NOT NULL) 
    
    0 讨论(0)
提交回复
热议问题