Sql Like to RegEx

若如初见. 提交于 2019-11-29 13:31:43

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.

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.

Pranay Rana

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

PATINDEX (Transact-SQL)

that will run very slow on a large table, parsing the string column on each and every row.

if you can change the table, it might be better to split that string column into different columns, so you can have a more traditional WHERE country=... AND sessionid=..., you could even add and use an index this way.

Use the RegEx functionality within SQL# which is free. I've not had any performance issues with this approach.

Not out of the box. The closest you get to a regex search in SQL Server is PATINDEX(), but that doesn't do regular expressions.

If you really wanted this, you'd have to make a CLR function to run the regex instead. You may be concerned about speed, but I would say that almost nothing you would do which concerns regular expressions or patterns would be able to use an index anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!