Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exact length column in SQL Server

Is there a way to make a column have a contraint of exactly so many characters? I have a string of 152 characters, and want the column to only accept values that are 152 in length, not 151, not 153. I know char can handle the overflow, but what about the minimum version?

like image 861
Mike Flynn Avatar asked Mar 05 '23 07:03

Mike Flynn


1 Answers

Add a check constraint which asserts that the length of the incoming string is exactly 152 characters:

ALTER TABLE [dbo].[YourTable] WITH CHECK
ADD CONSTRAINT [cnstr] CHECK (LEN(LTRIM([col])) = 152);
like image 67
Tim Biegeleisen Avatar answered Mar 12 '23 10:03

Tim Biegeleisen