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 2008 Forums
 Transact-SQL (2008)
 Regular Expression in where clause....?

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2014-09-15 : 10:36:03
I have a regular expression as shown below. Can someone please help me use this pattern via a SQL query?

pattern = ^[A-Za-z0-9_.-]+@([A-Za-z0-9-]+\.)+[A-Za-z0-9]{2,4}$

SELECT First_Name, Last_Name, Email FROM Contact
WHERE Email IS NOT NULL
AND Email NOT LIKE '^[A-Za-z0-9_.-]+@([A-Za-z0-9-]+\.)+[A-Za-z0-9]{2,4}$'

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-09-15 : 11:13:56
Unfortunately, T-SQL does not support regular expressions. There is a limited set of pattern matching capabilities via the LIKE and PATINDEX functions. See the available features here: http://technet.microsoft.com/en-us/library/ms187489(v=sql.105).aspx It is very very limited - for your example for instance, there are no quantifiers.

Full support for regular expressions would be on the top of my wish list. But, reasons still not clear to me, Microsoft is not asking me for my opinion!!

An alternative is to create a SQL CLR function - in which case you will have access to all the capabilities of the .net class System.Text.RegularExpressions.Regex. Here is a tutorial on how to do that: https://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/
Go to Top of Page
   

- Advertisement -