Re: to_char function returning wrong data

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sreeni Survi <sreenisurvi(at)gmail(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: to_char function returning wrong data
Date: 2019-02-04 17:31:59
Message-ID: 8046.1549301519@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Sreeni Survi <sreenisurvi(at)gmail(dot)com> writes:
> Below code has caused my data to be wiped off a table as my where clause
> depends on the below returned value.

> *select to_char(current_date - interval '5 weeks','IYYYWW') ;*
> *201953*

(For the archives, current_date - interval '5 weeks' is currently
'2018-12-31 00:00:00')

There's nothing wrong with to_char; it did what you told it to.

The problem here is that you're using ISO year numbering along with
non-ISO week numbering. You should have written 'IYYYIW', which
would give consistent results:

regression=# select to_char(current_date - interval '4 weeks','IYYYIW') ;
to_char
---------
201902
(1 row)

regression=# select to_char(current_date - interval '5 weeks','IYYYIW') ;
to_char
---------
201901
(1 row)

regression=# select to_char(current_date - interval '6 weeks','IYYYIW') ;
to_char
---------
201852
(1 row)

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2019-02-04 20:36:59 Re: BUG #15611: pg_dump output changes after doing a restore with certain views
Previous Message Andrew Gierth 2019-02-04 17:25:19 Re: to_char function returning wrong data