2

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 Answer 1

1

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)
);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, haven't had a chance to try it myself but im assuming this would be valid? (sorry for poor formatting) CHECK (EXTRACT (MONTH FROM exdate) = 6) CHECK (EXTRACT (YEAR FROM exdate) = 2015) CHECK (EXTRACT (DAY FROM exdate) > 0) CHECK (EXTRACT (DAY FROM exdate) < 31)
@ayoolaadedeji looks about right - probably missing commas between the constraints though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.