frustrationmultiplied
frustrationmultiplied

Reputation: 479

I need help writing a check constraint on a smalldatetime column

I need to add a Check constraint to a Column named Departure. This column has the smalldatetime data type.

The Check Constraint should state that: The date and time entered in the Departure column must be at least 6 hours from whatever the current time is when the date is being entered.

Can anyone help with the code.

Thank you

Upvotes: 1

Views: 169

Answers (1)

Yannick Meeus
Yannick Meeus

Reputation: 5900

This should do it:

ALTER TABLE [YourTableName]
ADD CONSTRAINT DepartureLaterThan6Hours CHECK ([Departure] > dateadd(HOUR, 6, GetDate()));

Upvotes: 2

Related Questions