Search Postgresql Archives

Re: date ranges

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

              http://www.postgresql.org/docs/faq

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux