I have a VARCHAR(30)
column in a Microsoft SQL Server database. I'd like to add a CHECK constraint that does not allow a value in the column to be less than 3 characters. What is the expression I must use?
Asked
Active
Viewed 3.1k times
18

marc_s
- 732,580
- 175
- 1,330
- 1,459

Jake Petroules
- 23,472
- 35
- 144
- 225
1 Answers
29
use:
ALTER TABLE [dbo].[YOUR_TABLE]
ADD CONSTRAINT [MinLengthConstraint] CHECK (DATALENGTH([your_column]) > 2)
Reference:

OMG Ponies
- 325,700
- 82
- 523
- 502
-
2Perfect, thanks for providing the note on DATALENGTH vs LEN as well. – Jake Petroules Dec 13 '10 at 05:35