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.
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.
if i want to see only the data contains special characters(apart from a-zA-Z0-9)
ReplyDelete