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

前端 未结 1 1089
青春惊慌失措
青春惊慌失措 2020-12-20 12:30

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

相关标签:
1条回答
  • 2020-12-20 13:06

    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 讨论(0)
提交回复
热议问题