Sql – How to make SQL Server 2008 Check Constraint of a table Allow Only Certain characters

sqlsql serversql-server-2008tsql

I'd like to create a check constraint for a table in SQL 2008 which would allow A-Z characters (non case sensitive), numbers, hyphen (-), dot(.), space and underscore (_).
Below is my current expression:
([company_code] not like '%[^A-Za-z0-9_ .+]%').

It fulfills all the above requirements except for hyphen. How could I make the expression allowing hyphen as well?

Best Answer

You can use an ESCAPE clause:

not like  '%[^A-Za-z0-9_ .+\-]%' escape '\'

The character after the escape character will be matched literally.