how to use substring to get the values in bold

  • Hello All,

    Could you please tell me how to get the value after 'between', before 'and' i.e., BOKSAM KAND LAMOG also after 'and' and before '('

    want to get the bold values (between, and are case senstive)

    for example MatterColumn has value "Collapsed Statue: Matured Life # 007812 between BOKSAM KAND LAMOG and Bill APPER SRIM (Goper)"

    right now i am doing this once is working and another one is saying issue with length character in substring

    rtrim (ltrim (SUBSTRING(MatterColumn,CHARINDEX('between',MatterColumn COLLATE Latin1_General_CS_AI)+8, ( Charindex(' and ', MatterColumn COLLATE Latin1_General_CS_AI,CHARINDEX('between',MatterColumn COLLATE Latin1_General_CS_AI)+8) - CHARINDEX('between',MatterColumn COLLATE Latin1_General_CS_AI)-8 ) ) )) Part1,

    rtrim (ltrim (SUBSTRING(MatterColumn,CHARINDEX(' and ',MatterColumn COLLATE Latin1_General_CS_AI)+5, ( Charindex(' (', MatterColumn COLLATE Latin1_General_CS_AI,CHARINDEX(' and ',MatterColumn COLLATE Latin1_General_CS_AI)+8) - CHARINDEX(' and ',MatterColumn COLLATE Latin1_General_CS_AI)-5 ) ) )) Part2

    please help me

    thanks in advance

    asitti

  • The problem you're likely getting is invalid values for the start or length of the SUBSTRING.

    Find the start value and length first and then test it to see if it's valid. If it is, then conditionally use it to do the SUBSTRING.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • You also need to ensure that 'between', 'and' and '(' cannot appear in the text you are trying to extract.

    I'd suggest doing this in stages to facilitate future maintenance.

    Definitely as has already been suggested obtain the string indices first - then check in case those terminators are not present.

    If there can be more than one instance then you probably will find it easier to follow the code if you store intermediate substrings in some working variables. That way you can look for the first or last instance of each and discard anything before the first or after the last.

    e.g. you go from

    Collapsed Statue: Matured Life # 007812 between BOKSAM KAND LAMOG and Bill APPER SRIM (Goper)

    everything after first 'between '

    BOKSAM KAND LAMOG and Bill APPER SRIM (Goper)

    everything before last ' ('

    BOKSAM KAND LAMOG and Bill APPER SRIM

    now you just need to worry about the ' and ' - how you handle that will depend whether ' and ' can appear in either string.

    If it can appear in the first section but not the second take everything before the last ' and ' and everything after the last one.

    If it can appear in the secondsection but not the first take everything before the first ' and ' and everything after the first one.

    If it can't appear in either - use whichever of the above is easier.

    If it can appear in both you will need to find some other way to identify the text you want..

  • Here's a method to parse the string by using a second delimited string of the keywords themselves used as the delimiting values. The strings are split using the DelimitedSplit8K function.

    DECLARE

    @s-2 VARCHAR(MAX)

    ,@Split CHAR(1)

    ,@KeyWords VARCHAR(50)

    SET @Split = ' '

    SELECT

    @s-2 = 'Collapsed Statue: Matured Life # 007812 between BOKSAM KAND LAMOG and Bill APPER SRIM (Goper)'

    ,@KeyWords = 'between,and,(Goper)'

    --other variations for testing

    --@S = 'Collapsed Statue: XXXX YYYYY Matured Life # 007812 between BOKSAM KAND LAMOG and Bill APPER SRIM (Goper)'

    --,@KeyWords = 'Statue:,between,and,(Goper)'

    --@S = 'Collapsed Statue: Matured Life # 007812 between BOKSAM KAND LAMOG and Bill APPER SRIM (Goper) XXXX YYYYY'

    --@S = 'Collapsed Statue: Matured Life # 007812 between BOKSAM ZZZZZ KAND LAMOG and Bill YYY APPER SRIM (Goper) XXXX YYYYY'

    --,@KeyWords = 'Collapsed,YYYYY'

    --,@KeyWords = '#,and,(Goper)'

    IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL

    DROP TABLE #TempTable

    CREATE TABLE #TempTable (

    [n1] INT NOT NULL,

    [i1] VARCHAR(50) NULL,

    [n2] INT NULL,

    [i2] VARCHAR(50) NULL,

    PRIMARY KEY (n1))

    INSERT INTO #TempTable

    SELECT

    dsk1.ItemNumber AS n1

    ,dsk1.Item AS i1

    ,dsk2.ItemNumber AS n2

    ,dsk2.Item AS i2

    FROM

    dbo.tvfDelimitedSplit8K(@S,' ') AS dsk1

    LEFT OUTER JOIN

    (

    SELECT

    Item

    ,ItemNumber

    FROM

    dbo.tvfDelimitedSplit8K(@KeyWords,',')

    ) AS dsk2

    ON dsk1.Item = dsk2.Item

    SELECT

    t1.i1 AS item

    FROM

    #TempTable AS t1

    INNER JOIN

    #TempTable AS t2

    ON t2.n1 = (SELECT n1 FROM #TempTable WHERE n1 > 0 GROUP BY n1 HAVING MIN(n2)=1)

    INNER JOIN

    #TempTable AS t3

    ON t3.n1 = (SELECT n1 FROM #TempTable WHERE n1 > 0 GROUP BY n1 HAVING MAX(n2)=(SELECT COUNT(n2) FROM #TempTable))

    WHERE

    t1.n1 > t2.n1

    AND t1.n1 < t3.n1

    AND t1.n2 IS NULL

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply