Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding upper case letter only constraint in SQL Server

create table EmployeeStatistics  
(  
     EmployeeIdNo int,  
     Name varchar(30),  
     Salary integer,  
     Benefits integer,  
     Position varchar(25),  
     Allowance integer,   
     DateOfBirth integer,  
     Telephone integer,     
     primary key (EmployeeIdNo)  
);

I want Position to accept only upper case letters. Which constraint to add?

alter table EmployeeStatistics
#add constraint
like image 387
Farhaan Avatar asked Dec 10 '22 21:12

Farhaan


1 Answers

You can use a check constraint where you compare the value with the upper case value using a binary collation or a case sensitive collation.

alter table dbo.EmployeeStatistics 
  add constraint CK_EmployeeStatistics_Position
  check (upper(Position) = Position collate Latin1_General_BIN2)
like image 166
Mikael Eriksson Avatar answered Dec 31 '22 22:12

Mikael Eriksson