Hello readers, Here are going to create a Check Constraint in SQL Server to allow only Alphabets in Column.

Check Constraint in SQL Server to  in the column:

Sometimes you face a situation to allow alphabets in columns. This is the real-world scenario.  Here I am going to create a Check Constraint that will prevent any character other than Alphabets.

Example:

Create Table:

CREATE TABLE EmployeeDetails
(ID INT, EmployeeName VARCHAR(100),
CONSTRAINT chkEmployeeName CHECK (EmployeeName NOT LIKE '%[^A-Z]%'))
GO

In the above script, i have created a Check Constraint called chkEmployeeName that will check if EmployeeName is not an Alphabet.

Test Check Constraint:

INSERT INTO [dbo].[EmployeeDetails] (ID, EmployeeName)
VALUES (1, 'DebugOnWeb')
GO

If you run the above run the above query it will run without any error, Now run the following query.

INSERT INTO [dbo].[EmployeeDetails] (ID, EmployeeName)
VALUES (1, 'DebugOnWeb 1')
GO

If you run the above following error will be raised.

Allow only Alphabets

So this simple Constraint solve the problem of allowing Alphabets in column

View More:

Conclusion:

I hope this Check Constrain of allowing only Alphabets in the column will solve your real-world problems. I would like to have your feedback, comments, and suggestions.

Thank You.

Leave a Comment