Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use regular expression in TSQL?

I'd need to have a regular expression so that I can find all invalid email addresses in our database table.

SELECT *
FROM dbo.[Users] AS u 
WHERE u.EmailAddress not like 'regular expression of valid email addresses'

Does SQL Server support regular expressions? If so, how could I write one for email address?

like image 273
The Light Avatar asked Dec 07 '11 14:12

The Light


People also ask

Can I use regular expression in SQL?

You can use RegEx in many languages like PHP, Python, and also SQL. RegEx lets you match patterns by character class (like all letters, or just vowels, or all digits), between alternatives, and other really flexible options.

Can we use regular expression in MySQL?

MySQL supports another type of pattern matching operation based on the regular expressions and the REGEXP operator. It provide a powerful and flexible pattern match that can help us implement power search utilities for our database systems. REGEXP is the operator used when performing regular expression pattern matches.


1 Answers

T-SQL does not support regular expressions. You can however create a .net CLR function that will add this ability.

This might help.
http://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/

like image 90
Doug Chamberlain Avatar answered Sep 27 '22 16:09

Doug Chamberlain