
September 23rd, 2004, 03:15 PM
|
|
Contributing User
|
|
Join Date: Jun 2002
Location: Pennsylvania, USA
|
|
|
How to select string of specific pattern type
Hi,
I have a column in my table that is in the format %-_-___%.
Another way to think of it is 'junk1 dash char dash char char char char junk2'
Ex 1: 3gnrs9-C-5001 Late
Ex 2: 009-8-H63v-Terminated
What I need to do is return the part of the string that matches from the beginning up to the last wildcard. Or in other words 'junk1 dash char dash char char char char' Junk2 needs to be trimmed.
Ex 1 would return: 3gnrs9-C-5001
Ex 2 would return: 009-8-H63v
The first character of the last wildcard can be anything (number, letter, space, dash, ect). I've read the docs on MS SQL string functions, but can't seem to put them together to return what I need. I thought PATINDEX might work, but I need the index at the end of the pattern, not the beginning. I thought of using CHARINDEX, but the first char after the pattern is not static, so I cant search on it. LEFT wont work because the part before junk2 is not a static length.
I'll be using the returning string in the "ON" portion of a join. Any help or advice would be appreciated!
Thanks!
|