how to create date check in postgresql

185 views Asked by At

When creating a table in postgres of type time its very simple to set a time check, to make sure that what is entered has to be between two times like so:

extime       TIME           NOT NULL,
check(extime > '09:00:00' and extime < '18:00:00')

I want to do something similar with a date field

exdate       DATE           NOT NULL,

to have it between the beginning and end of june.

is anybody able to give me some help?

1

There are 1 answers

2
Mureinik On BEST ANSWER

IMHO, the easiest approach would be to extract the month from the date, and make sure it's in June:

CREATE TABLE mytable (
    exdate DATE NOT NULL,
    CHECK (EXTRACT (MONTH FROM exdate) = 6)
);