开发者

How can I create a CHECK constraint on a VARCHAR column in SQL Server specifying a minimum data length?

开发者 https://www.devze.com 2023-01-30 01:00 出处:网络
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

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?


use:

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

Reference:

  • DATALENGTH vs LEN
  • SQL SERVER – 2005 Constraint on VARCHAR(MAX) Field To Limit It Certain Length
0

精彩评论

暂无评论...
验证码 换一张
取 消