Faisal Thayyil
Faisal Thayyil

Reputation: 137

How to restrict SQL Server from accepting empty string

I have a table in SQL Server which has a constraint like Not null. When I enter new entry if I am not filling anything, SQL Server pops error message preventing Null value.

But once I enter one record and later if I edit that record by just deleting the data in this column, SQL Server is accepting the empty string.

Upvotes: 2

Views: 2413

Answers (2)

Ramesh Kharbuja
Ramesh Kharbuja

Reputation: 407

By UI you can add constraint by following step

  • 1.open the table in Design view
    1. Right Click any column and select "Check Constraints"
    2. Edit"Expression" and set the minimum lenth of any field as len(desiredField)>=1

Upvotes: 1

Richard Schneider
Richard Schneider

Reputation: 35477

You can use a CHECK CONSTRAINT

ALTER TABLE table
ADD CONSTRAINT chkNotEmpty CHECK (LEN(col) > 0 );

Upvotes: 5

Related Questions