3

I am trying to find a way to validate data in my columns using regex CHECK constraints but have so far only come across the LIKE keyword that has an extremely limited subset of regex syntax. I'd like to know if there is a way to use regex in check constraints?

I am using SQL Server 2005 and 2008.

2
  • 1
    So far, I've only found one DBE that supports posix regex constraints and it is Oracle (Reference: stackoverflow.com/questions/35157662/…). MS SQL and DB2 look like they have their own wildcard variant, which is unfortunate as you loose consistency of constraints between layers :( Commented Feb 2, 2016 at 16:54
  • 1
    Relevant QA on the DBA site... dba.stackexchange.com/q/266438/3690 Commented May 6, 2020 at 21:08

2 Answers 2

4

I think you can create a CLR scalar UDF which can use the .Net framework RegEx classes, and then call that from your check constraint.

In fact, this article discusses this scenario (search for "Check Constraint", then move backwards through the article to find implementation details).

Sign up to request clarification or add additional context in comments.

Comments

0

I'm a little late to the thread here but check out my article about how to do this:

http://xmlsqlninja.blogspot.com/2013/09/mdqregex-clr-functions-part-3-regex.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.