|
i want to select a string between two known strings but i dont know the length of the string to be selected like
(comments are locked)
|
You could of course do it with Regular Expressions as well, but then you'd have to install a CLR to support the Regex functions. You can also try to just replace the two known strings with empty string which will let you get the desired string as a leftover. The downside of this approach is that if the part of desired value itself also contains your known prefix and/or suffix
Jun 09 '11 at 01:52 PM
Oleg
But that won't cover the string in my example, will it? It will select the text before and after the known strings as well, not just the inbetween string.
Jun 09 '11 at 02:31 PM
Magnus Ahlkvist
@Magnus Ahlkvist No it will not. Your snippet is definitely better. I just made a silly assumption that the desired string is always sandwiched between 2 known strings. If this is not the case then using replace is no good.
Jun 10 '11 at 11:56 AM
Oleg
+1 And as @Magnus Ahlkvist mentioned the Regular Expressions, if you can have multiple occurrences of the strings in the source string and want to return all the instance of the text between the Start/End strings, then the RegEx implementation will the quickest and easiest approach. You can take a look on sample implementation in my post SQL Server 2005 and SQL Server 2008 Regular Expressions and pattern matching
Jun 10 '11 at 12:44 PM
Pavel Pawlowski
(comments are locked)
|
+1 candy is dandy but liquor is quicker
Jun 11 '11 at 10:58 PM
Scot Hauder
(comments are locked)
|
|
As a variation to the answer from @magnus ahlkvist ...
(comments are locked)
|
|
@Jonathan Allen: Unless I'm missing something, shouldn't you be subtracting LEN(@firstpatn) from the second part of that equation rather than LEN(@secondpatn) when you're selecting the substring? e.g.
It appears that it worked in the example you provided because @FirstPatn and @SecondPatn happened to be the same length. Other than that, your example made my life a lot easier, thanks for the answer!
(comments are locked)
|

