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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 PATINDEX wildcard syntax

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'?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 included
INSERT INTO @vartab (String) values ('kekrr/jocpjerovjpcrjv')--no first space excluded
INSERT INTO @vartab (String) values (' kek rr/jocpjerovjpcrjv')--space before '/' excluded
INSERT INTO @vartab (String) values (' kekrrjocpjerovjpcrjv')--no '/' excluded
INSERT INTO @vartab (String) values (' kekrr/jocp jerovj pcrjv')--after '/'space included

SELECT * FROM @vartab
WHERE LEFT(String,1)=' '
AND CHARINDEX('/',String) <>0
AND CHARINDEX(' ',SUBSTRING(String,2, CHARINDEX('/',String)-1)) =0

output
------------------------------------------

ID String
----------- ------------------------
1 kekrr/jocpjerovjpcrjv
5 kekrr/jocp jerovj pcrjv

Go to Top of Page

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.

Go to Top of Page

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' UNION
SELECT 'this is the code: 90/giggle' UNION
SELECT 'hvy/ikls oodpzx' UNION
SELECT '/loop de loop' UNION
SELECT 'play this tune/' UNION
SELECT ' 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
Go to Top of Page

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?
Go to Top of Page

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.

Go to Top of Page
   

- Advertisement -