Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to use substring to get the values in bold Expand / Collapse
Author
Message
Posted Friday, November 16, 2012 6:53 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 29, 2014 4:55 PM
Points: 139, Visits: 279
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
Post #1385894
Posted Sunday, November 18, 2012 9:54 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:14 AM
Points: 36,735, Visits: 31,186
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1386059
Posted Monday, November 19, 2012 5:19 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 1:11 AM
Points: 201, Visits: 810
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..


Post #1386311
Posted Monday, November 19, 2012 8:55 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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 VARCHAR(MAX)
,@Split CHAR(1)
,@KeyWords VARCHAR(50)

SET @Split = ' '

SELECT
@S = '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


Post #1386659
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse