17

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?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Jake Petroules
  • 22,632
  • 35
  • 139
  • 223

1 Answers1

28

use:

ALTER TABLE [dbo].[YOUR_TABLE]
ADD CONSTRAINT [MinLengthConstraint] CHECK (DATALENGTH([your_column]) > 2)

Reference:

OMG Ponies
  • 314,254
  • 77
  • 507
  • 490