Re: date ranges - Mailing list pgsql-general

From brian
Subject Re: date ranges
Date
Msg-id 46B47C91.60101@zijn-digital.com
Whole thread Raw
In response to date ranges  (jo <jose.soares@sferacarta.com>)
List pgsql-general
jo wrote:
> Hello,
>
> I would like to know if there's a simple way to customize the range for
> dates,
> to avoid people insert dates before 1900 and after 2020, for example.
>

test=# CREATE TABLE foo (
   id SERIAL PRIMARY KEY,
   dt DATE,
   CHECK (
     EXTRACT('year' FROM dt) >= 1900 AND EXTRACT('year' FROM dt) < 2020
   )
);

test=# INSERT INTO foo (dt) VALUES ('1984-03-02');
INSERT 0 1
test=# INSERT INTO foo (dt) VALUES ('1884-03-02');
ERROR:  new row for relation "foo" violates check constraint "foo_dt_check"
test=# INSERT INTO foo (dt) VALUES ('2024-03-02');
ERROR:  new row for relation "foo" violates check constraint "foo_dt_check"

There's probably a more elegant way to do this.
brian

pgsql-general by date:

Previous
From: Raymond O'Donnell
Date:
Subject: Re: date ranges
Next
From: Michael Glaesemann
Date:
Subject: Re: date ranges