Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Like to RegEx

Is there a good way to convert Regular Expression into LIKE inside a Function (MSSQL)? The sproc does not get more complicated than this:

(country\=)(?<Country>[\w\d]+)(\&sessionid\=)(?<SessionId>.+)

The groups will not be used in the LIKE, they are there for another purpose.

I would like to use this inside a sproc late like:

SELECT * FROM [Table]
WHERE test_regex(regex, 'text') = 1

Where the regex is a part of [Table]

like image 813
Andreas Avatar asked Jul 15 '10 12:07

Andreas


3 Answers

Check this function of sql. It may help you to achieve your task : PATINDEX('%[^0-9\.]%',@Input)

PATINDEX (Transact-SQL)

like image 114
Pranay Rana Avatar answered Oct 08 '22 20:10

Pranay Rana


It is possible to add Regular Expression parsing to SQL server, using a CLR function. This is possible from SQL 2005 and up. See here for a great example.

like image 4
edosoft Avatar answered Oct 08 '22 21:10

edosoft


I'd imagine

SELECT ...
FROM [Table]
WHERE col LIKE 'country=[A-Za-z0-9]%&sessionid=[A-Za-z0-9]%'

might be close enough? If the aim is just to bring back records with non blank country and sessionid values.

If the value in the column wouldn't necessarily start with 'country' you'd have to use WHERE col LIKE '%country=[A-Za-z0-9]%&sessionid=[A-Za-z0-9]%' which could slow things down considerably as per KM's answer.

like image 4
Martin Smith Avatar answered Oct 08 '22 22:10

Martin Smith