1

I want to create a new table that following code below;

create table dwh.dim_produk (
sk_produk serial primary key,
kode_produk varchar(25),
nama_produk varchar(50),
kode_kategori varchar(50),
nama_kategori varchar(50),
date_from date not null default current_timestamp,
date_to date not null default '12/31/9999');

but i get result such as;

SQL Error [22008]: ERROR: date/time field value out of range: "12/31/9999"
Hint: Perhaps you need a different "datestyle" setting.
Position: 253

thank in advance

3
  • That is a strange default date. Why not use NULL? And the default date format is 9999-12-31 Commented Jun 21, 2021 at 3:42
  • If you really think that the 9999 year is better then the present one: make_date(year => 9999, month => 12, day => 31) Commented Jun 21, 2021 at 3:53
  • The issue is due to your datestyle setting. In psql do show datestyle. You have one that has a DMY ordering so the 31 in the string is out of range. Change the value to 31/12/999 and it will work. Commented Jun 21, 2021 at 14:43

1 Answer 1

3

If you want a date that is "in the future", use infinity

date_from date not null default current_date,
date_to date not null default 'infinity'

Or if you really want a date in the year 9999, use a proper DATE literal formatted using ANSI style:

date_from date not null default current_date,
date_to date not null default DATE '9999-12-31'

Alternatively you can represent a range using the daterange data type:

valid_during daterange not null default daterange(current_date,null)

A null for the upper range means "infinity" as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.