Wednesday, October 22, 2014

Regex in sql server

The like statement is sometimes a bit frustrating, because it has nothing to do with the regular expression you know from development in e.g. .NET. Similar to like is patindex.

The proprietary placeholders are:

  • % for 0-n characters
  • _ for 1 character
  • [] ranges
    • e.g. [a-f] for any single character between a and f inclusively
    • e.g. [abcdef] for any single character between a and f inclusively
    • e.g. [[] for [, - can be set as first item in list to use it, []], [_], [%]
  • [^] ranges (any character which is not in the range.

Really using Regex:
  • VBScript, COM
    http://blogs.msdn.com/b/khen1234/archive/2005/05/11/416392.aspx
  • Project for native regex
    http://www.codeproject.com/Articles/4733/xp-pcre-Regular-Expressions-in-T-SQL
  • CLR function
    http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
    http://msdn.microsoft.com/en-us/library/w2kae45k%28v=vs.80%29.aspx

kind regards,
Daniel


EDIT: in contrary to mysql ... there you can use it in the where statement "native". E.g.:
select * from xyTable where name regexp 'asdf$' 

No comments: