SQL Server: Validating That End Date Exceeds Start Date

I have a table with two columns representing dates: one for the start and the other for the end, both of which can be null. When both dates are provided, I need to ensure that the end date comes after the start date. How can I implement a check constraint in SQL Server 2008 to enforce this condition?

hey jasper, have u tried using a check constraint like CHECK((start_date is null) or (end_date is null) or (start_date < end_date))? seems a neat solution. any thoughts on potential issues or other methods?

hey, i once had a similar issue and chose to use a trigger instead. it allowed me to include customized error msgs and extra logic if needed. it’s a bit more code, but sometimes triggers offer the flexibility you need.

An alternative approach involves creating a persisted computed column that verifies the date relationship and then enforcing a check constraint on that column. For example, you could create a computed column that returns 1 when the validation condition is met – that is, when either or both dates are null or when the start date is less than the end date – and 0 otherwise. A check constraint can then ensure that the computed column always equals 1. This method provides clarity and allows for future extensions if additional validation logic is needed.

hey jasper, have u thought abt using a stored proc to wrap u date validations? i find it gives more room to handle edge cases and customize error msgs when dates mix up. what do u think about adding more business logic thru procs?

hey jasper, you might try a udef fn in your constraint to centralise the logic. it keeps the check tidy and reusable, though you’ll need to handle nulls carefully. might be an interesting alternate approach