From: Tom Lane <tgl@sss.pgh.pa.us>
To: Sreeni Survi <sreenisurvi@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: to_char function returning wrong data
Date: Mon, 04 Feb 2019 12:31:59 -0500
Message-ID: <8046.1549301519@sss.pgh.pa.us> (raw)
In-Reply-To: <CAD91y4RLdbXvr+oNoN++Wmn2SjxHYRJZnT95f=BPZcSO0OBTxg@mail.gmail.com>
References: <CAD91y4RLdbXvr+oNoN++Wmn2SjxHYRJZnT95f=BPZcSO0OBTxg@mail.gmail.com>
Sreeni Survi <sreenisurvi@gmail.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, sreenisurvi@gmail.com
Subject: Re: to_char function returning wrong data
In-Reply-To: <8046.1549301519@sss.pgh.pa.us>
References: <CAD91y4RLdbXvr+oNoN++Wmn2SjxHYRJZnT95f=BPZcSO0OBTxg@mail.gmail.com> <8046.1549301519@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox