Search

Showing posts with label Check constraint to allow alphabets and numbers only in SQL Server. Show all posts
Showing posts with label Check constraint to allow alphabets and numbers only in SQL Server. Show all posts

Friday, September 7, 2012

Check constraint to allow alphabets and numbers only in SQL Server

How to create a check constraint to allow alphabets i.e. A-Z characters only or alphabets + numbers with no special characters in Sql Server?
Here is the scenario, you have column with varchar/char datatype and you don’t want user to any Special characters like ~, !, #, $, %, ^.
Check constraint for allowing Alphabets only 

ALTER TABLE TableName ADD CONSTRAINT Only_Characters CHECK ColumnName NOT LIKE '%[^A-Z ]%' 


Check constraint for allowing Alphabets  + Numbers  only

ALTER TABLE TableName ADD CONSTRAINT Only_Characters_And_Numebrs CHECKColumnName NOT LIKE '%[^A-Z0-9 ]%' 


Note: Remember to add extra space.