Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Regular Expressions

I created the following SQL regex pattern for matching an ISBN:

CREATE RULE ISBN_Rule AS @value LIKE 'ISBN\x20(?=.{13}$)\d{1,5}([-])\d{1,7}\1\d{1,6}\1(\d|X)$'

I used the following values as test data; however, the data is not being committed:

ISBN 0 93028 923 4 | ISBN 1-56389-668-0 | ISBN 1-56389-016-X

Where am I wrong?

like image 919
Sudantha Avatar asked Feb 06 '11 05:02

Sudantha


People also ask

Can you use regular expressions 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.

What is regular expression 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.

What are regular expressions in mysql?

A regular expression describes a set of strings. The simplest regular expression is one that has no special characters in it. For example, the regular expression hello matches hello and nothing else. Nontrivial regular expressions use certain special constructs so that they can match more than one string.


2 Answers

You can do this using LIKE.

You'll need some ORs to deal with the different ISBN 10 and 13 formats

For the above strings:

LIKE 'ISBN [0-9][ -][0-9][0-9][0-9][0-9][0-9][ -][0-9][0-9][0-9][ -][0-9X]'
like image 194
gbn Avatar answered Sep 20 '22 23:09

gbn


The LIKE operator in SQL Server isn't a regex operator. You can do some complicated pattern matching, but its not normal regex syntax.

http://msdn.microsoft.com/en-us/library/ms179859.aspx

like image 42
kelloti Avatar answered Sep 21 '22 23:09

kelloti