Search code examples
sqlsql-serverstringt-sqlcheck-constraints

In a T-SQL check constraint, how to allow either a specific pattern or the empty string


I have a phone-number pattern in place now. I'd like to allow the empty string temporarily as a valid value. What is the pattern for that? Here's the existing check:

( ([fax] like '[1-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9][0-9]'))

How would that pattern be changed to accept the empty string as well?


Solution

  • How would that pattern be changed to accept the empty string as well?

    You could just use or:

    (   
        [fax] = ''
        or [fax] like '[1-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9][0-9]'
    )