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?

Mureinik
  • 297,002
  • 52
  • 306
  • 350

1 Answers1

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)
);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 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) –  Nov 25 '14 at 18:00
  • @ayoolaadedeji looks about right - probably missing commas between the constraints though. – Mureinik Nov 25 '14 at 20:25