Tuesday, July 15, 2014

SQL SERVER – How to Allow Only Alphabets in Column – Create Check Constraint to Insert Only Alphabets


user can create a check constraint over column to allow insertion of alphabets in the column. Here is a sample script where I demonstrate how users can create constraint over column first so it only allows alphabets.

USE tempdb
GO
-- Create Test tableCREATE TABLE TestTable(ID INT, FirstCol VARCHAR(100),CONSTRAINT FirstCol CHECK (FirstCol NOT LIKE '%[^A-Z]%'))GO-- This will be successfulINSERT INTO TestTable (ID, FirstCol)VALUES (1, 'SQLAuthority')GO-- This will throw an errorINSERT INTO TestTable (ID, FirstCol)VALUES (1, 'SQLAuthority 1')GO-- Clean upDROP TABLE TestTable
GO

No comments:

Post a Comment

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...