LiamHarries
LiamHarries

Reputation: 570

SQL Server string constraints

I have a SQL Server database with a time column which can only be filled with the text "am" or "pm" and I'm having trouble finding a constraint that would allow me to do this.

Upvotes: 7

Views: 12212

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

For SQL server you can use CHECK constraint which allows you to define a predicate that all the rows must meet in order to enter the table. Like so:

ALTER TABLE TableName 
ADD CONSTRAINT CHK_ampm
CHECK(ColumnName IN ('am', 'pm'));

Upvotes: 19

Related Questions