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

Complete Authentication System in ASP.NET Core using JWT

JWT (JSON Web Token) authentication is widely used for securing APIs in modern applications. In this article, we will build a complete authe...