Jeigh Jeigh - 7 months ago 5
SQL Question

Ensuring a date entered is at least 3 years greater than another

I am trying to make a constraint to ensure that the

finishdate
is at least 3 years greater than the
startdate
.

I have had a look around but really do not know where to start?

Would i need to use a
dateadd
function?

Thanks

Answer

You could do it a number of ways, but since you asked about check constraints, something like this should work:

CREATE TABLE myTable
(
  id numeric(4),
  startdate date,
  finishdate date,
  CONSTRAINT check_date
  CHECK (finishdate >= add_months( startdate , 36 ))
);
Comments