Adding a minimum length constraint in SQL Server

This might seem fairly obvious, but using Len(ColumnName) can give unexpected results, as it returns the number of characters in a string instead of the number of bytes, i.e. it trims the text before it counts the characters. More details on that here.

To create a minimum length constraint, open up your table, right click Constraints and select New Constraint. Then enter something like…

(datalength([YourColumn])>(2))

top