Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions inside SQL Server

Tags:

I have stored values in my database that look like 5XXXXXX, where X can be any digit. In other words, I need to match incoming SQL query strings like 5349878.

Does anyone have an idea how to do it?

I have different cases like XXXX7XX for example, so it has to be generic. I don't care about representing the pattern in a different way inside the SQL Server.

I'm working with c# in .NET.

like image 899
Jack Avatar asked Dec 26 '09 19:12

Jack


People also ask

How does RegEx work in SQL?

A Regular Expression is popularly known as RegEx, is a generalized expression that is used to match patterns with various sequences of characters. A RegEx can be a combination of different data types such as integer, special characters, Strings, images, etc.

Does Azure SQL support RegEx?

Azure SQL Data Warehouse does not include support for Regular Expressions.

What's the difference between () and [] in regular expression?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.

What is expression in SQL Server?

An expression is a combination of one or more values, operators, and SQL functions that evaluate to a value. An expression generally assumes the datatype of its components.


1 Answers

You can write queries like this in SQL Server:

--each [0-9] matches a single digit, this would match 5xx SELECT * FROM YourTable WHERE SomeField LIKE '5[0-9][0-9]' 
like image 194
dan Avatar answered Sep 22 '22 17:09

dan