I need to extract a text that is surrounded by ***[some text]
strings, like in the following example:
some text
some text
***[some text]
THIS SHOULD BE EXTRACTED
***[some text]
some text
some text
some text
some text
some text
***[some text]
THIS SHOULD BE EXTRACTED TOO
***[some text]
some text
the output should be:
THIS SHOULD BE EXTRACTED
THIS SHOULD BE EXTRACTED TOO
I tried PATINDEX
like here, but couln't find the way to extract the string.
PATINDEX('%[*][*][*][[]%]%%[*][*][*][[]%]%',@Text)
I am looking forward to hearing any suggestions.
For the somewhat easier case raised in the comments you could do
;WITH T(C) AS
(
SELECT '
some text
some text
***[some text 1]
THIS SHOULD BE EXTRACTED
***[some text 2]
some text
some text
some text
some text
some text
***[some text 1]
THIS SHOULD BE EXTRACTED TOO
***[some text 2]
some text'
)
SELECT col.value('.','varchar(max)')
FROM T
CROSS APPLY (SELECT CAST('<a keep="false">' +
REPLACE(
REPLACE(C,'***[some text 2]','</a><a keep="false">'),
'***[some text 1]','</a><a keep="true">') +
'</a>' AS xml) as xcol) x
CROSS APPLY xcol.nodes('/a[@keep="true"]') tab(col)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With