Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
SoftFox
Starting Member
42 Posts |
Posted - 2008-02-08 : 03:42:10
|
I am using PATINDEX to search a string to try and extract a code contained in the string. This is to be applied to a column where the only way of identifying the code is by the "/" character. E.g Below i want to extract xxx/yyy. (However the code will not always take the form xxx/yyy, only the "/" we can be sure of). 'monkey says xxx/yyy is very nice'What i need to be able to searh for is a string that starts with a space, then is followed by any string of charcters but not containing a space and then followed by "/". The bit i cannot do is in italics. Below will search for a string that starts with a space, then is followed by any string of charcters, and then followed by "/". But this of course finds the first space in the sentence, afer "monkey":SELECT PATINDEX('% %/%', 'monkey says xxx/yyy is very nice')or I can search for a string that starts with a space, then is followed by a fixed number of characters that are not spaces, followed by "/". SELECT PATINDEX('% [^ ][^ ][^ ]/%', 'monkey says xxx/yyy is very nice')The problem is there can be different numbers of characters before the "/", e.g. could be x/yyy, xxxxxxxx/yyyy, xx/xxx/xxxxxx.Does anyone know how to do this? |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-08 : 04:00:00
|
What do you want to get from 'monkey says xxx/yyy is very nice'?MadhivananFailing to plan is Planning to fail |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-08 : 04:05:03
|
See if this is what you want:-DECLARE @vartab table(ID int IDENTITY(1,1),String varchar(1000))INSERT INTO @vartab (String) values (' kekrr/jocpjerovjpcrjv')--correct includedINSERT INTO @vartab (String) values ('kekrr/jocpjerovjpcrjv')--no first space excludedINSERT INTO @vartab (String) values (' kek rr/jocpjerovjpcrjv')--space before '/' excludedINSERT INTO @vartab (String) values (' kekrrjocpjerovjpcrjv')--no '/' excludedINSERT INTO @vartab (String) values (' kekrr/jocp jerovj pcrjv')--after '/'space included SELECT * FROM @vartabWHERE LEFT(String,1)=' 'AND CHARINDEX('/',String) <>0AND CHARINDEX(' ',SUBSTRING(String,2, CHARINDEX('/',String)-1)) =0output------------------------------------------ID String----------- ------------------------1 kekrr/jocpjerovjpcrjv5 kekrr/jocp jerovj pcrjv |
 |
|
SoftFox
Starting Member
42 Posts |
Posted - 2008-02-08 : 09:46:45
|
Madhivanan, i need to get the string xxx/yyy from 'monkey says xxx/yyy is very nice'visakh, i cant see how i would apply the above code to do this. I need to find the index of the start of the string "xxx/yyy" from the string "monkey says xxx/yyy is very nice", where "xxx/yyy" could vary e.g "x/yyy", "abcasdfasf234234/sfsdfsfd", "/eeee".. Essentially a string that starts with a space, is then followed by any number of characters which can be anything except spaces, followed by a "/". I can nearly get this ( see examples in original post: SELECT PATINDEX('% %/%', 'monkey says xxx/yyy is very nice') and SELECT PATINDEX('% [^ ][^ ][^ ]/%', 'monkey says xxx/yyy is very nice')), but i dont know how to specify any number of characters which can be anything but a space. |
 |
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-02-08 : 11:03:16
|
Here is an ugly solution:DECLARE @strings TABLE ( string VARCHAR(50) )INSERT @strings ( string )SELECT 'monkey says xxx/yyy is very nice' UNIONSELECT 'this is the code: 90/giggle' UNIONSELECT 'hvy/ikls oodpzx' UNIONSELECT '/loop de loop' UNIONSELECT 'play this tune/' UNIONSELECT ' this string ojasdf/asfd eidl/losk'SELECT string,RIGHT(LEFT( string, CHARINDEX( '/', string)-1), CASE CHARINDEX( ' ', REVERSE(LEFT( string, CHARINDEX( '/', string)-1))) WHEN 0 THEN LEN(LEFT( string, CHARINDEX( '/', string)-1)) ELSE CHARINDEX( ' ', REVERSE(LEFT( string, CHARINDEX( '/', string)-2))) END)+ '/'+ SUBSTRING( string, CHARINDEX( '/', string)+1, CHARINDEX( ' ', SUBSTRING( string, CHARINDEX( '/', string)+2, LEN(string)-CHARINDEX( '/', string)+1)))FROM @strings |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-08 : 13:30:59
|
quote: Originally posted by SoftFox Madhivanan, i need to get the string xxx/yyy from 'monkey says xxx/yyy is very nice'visakh, i cant see how i would apply the above code to do this. I need to find the index of the start of the string "xxx/yyy" from the string "monkey says xxx/yyy is very nice", where "xxx/yyy" could vary e.g "x/yyy", "abcasdfasf234234/sfsdfsfd", "/eeee".. Essentially a string that starts with a space, is then followed by any number of characters which can be anything except spaces, followed by a "/". I can nearly get this ( see examples in original post: SELECT PATINDEX('% %/%', 'monkey says xxx/yyy is very nice') and SELECT PATINDEX('% [^ ][^ ][^ ]/%', 'monkey says xxx/yyy is very nice')), but i dont know how to specify any number of characters which can be anything but a space.
tried my soln? |
 |
|
SoftFox
Starting Member
42 Posts |
Posted - 2008-02-11 : 06:20:24
|
Ugly though it is, it does the trick. Thankyou jdaman.If however anyone does know how to specify any number of characters that arent spaces in the PATINDEX function (similar to what can be done in a regular expression) it would be very useful to know. Then the expression below could be modified to get the result (xxx/yyyy):SELECT PATINDEX('% [^ ][^ ][^ ]/%', 'monkey says xxx/yyy is very nice')--currently this only works with a fixed number of non-space characters, here 3 of them, using [ ^]. What would be great would be to be able to specify any number of non-space characters. |
 |
|
|
|
|
|
|