PostgreSQL – Unexpected Behavior for to_date() with Week Number and Week Day

date formatpostgresql

I tried to convert a date string (year, week, day of week = 'YYYY-WW-D') into a date using the to_date() function.

I got these results (demo:db<>fiddle):

date string |  to_date()
-------------------------
'2019-1-1'  |  2019-01-01
'2019-1-2'  |  2019-01-01
'2019-1-3'  |  2019-01-01
'2019-1-4'  |  2019-01-01
'2019-1-5'  |  2019-01-01
'2019-1-6'  |  2019-01-01
'2019-1-7'  |  2019-01-01

'2019-2-1'  |  2019-01-08
'2019-2-2'  |  2019-01-08

It is not clear to me, why this results in the same date for every day of a week.

If I take the ISO format instead ('IYYY-IW-ID') this works as expected (demo:db<>fiddle):

date string |  to_date()
-------------------------
'2019-1-1'  |  2018-12-31
'2019-1-2'  |  2019-01-01
'2019-1-3'  |  2019-01-02
'2019-1-4'  |  2019-01-03
'2019-1-5'  |  2019-01-04
'2019-1-6'  |  2019-01-05
'2019-1-7'  |  2019-01-06

'2019-2-1'  |  2019-01-07
'2019-2-2'  |  2019-01-08

Why does it work for the ISO pattern, not for the standard pattern? What am I missing here?

Best Answer

This behavior is documented:

In to_timestamp and to_date, weekday names or numbers (DAY, D, and related field types) are accepted but are ignored for purposes of computing the result. The same is true for quarter (Q) fields.