Sudantha
Sudantha

Reputation: 16204

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?

Upvotes: 6

Views: 27927

Answers (4)

gbn
gbn

Reputation: 432261

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]'

Upvotes: 6

user557597
user557597

Reputation:

If it splits on | and doesen't strip whitespaces, its probably missing a space before ISBN and/or after (\d|X) here $ .. Also, I doubt this is the problem, but [- ] could be [ -]

edit: ok, well keep this in mind when you get a regex lib/control.

Upvotes: 1

RichardTheKiwi
RichardTheKiwi

Reputation: 107716

SQL Server 2005 does not support REGEX expressions out of the box, you would need OLE Automation or a CLR to provide that functionality through a UDF.

The only supported wildcards are % (any) and _ (one), and character range (or negation) matches using [] optionally [^]. So your expression

'ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$'

Means something very weird with the range [- ] and everything else being literal.

Upvotes: 4

kelloti
kelloti

Reputation: 8951

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

Upvotes: 6

Related Questions