SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to use substring to get the values in bold


how to use substring to get the values in bold

Author
Message
asita
asita
Say Hey Kid
Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)Say Hey Kid (664 reputation)

Group: General Forum Members
Points: 664 Visits: 337
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218412 Visits: 41996
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
crmitchell
crmitchell
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1266 Visits: 1794
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..
Steven Willis
Steven Willis
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2079 Visits: 1721
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



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search