pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
to_char function returning wrong data
3+ messages / 3 participants
[nested] [flat]

* to_char function returning wrong data
@ 2019-02-04 17:16  Sreeni Survi <sreenisurvi@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Sreeni Survi @ 2019-02-04 17:16 UTC (permalink / raw)
  To: pgsql-bugs

Hi,

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*

If the number of weeks is 4 or 6 or 3 it returns correct value.

select to_char(current_date - interval '4 weeks','IYYYWW') ;
201901

select to_char(current_date - interval '6 weeks','IYYYWW') ;
201852

This issue can have a serious effect.

Thanks,
Sreeni

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: to_char function returning wrong data
@ 2019-02-04 17:25  Andrew Gierth <andrew@tao11.riddles.org.uk>
  parent: Sreeni Survi <sreenisurvi@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Andrew Gierth @ 2019-02-04 17:25 UTC (permalink / raw)
  To: Sreeni Survi <sreenisurvi@gmail.com>; +Cc: pgsql-bugs

>>>>> "Sreeni" == Sreeni Survi <sreenisurvi@gmail.com> writes:

 Sreeni> Hi,
 Sreeni> Below code has caused my data to be wiped off a table as my
 Sreeni> where clause depends on the below returned value.

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

That's because IYYY is intended for use with IW, not with WW. The output
is correct, it's the query that's wrong.

There are several different definitions of "week number", and IW is the
ISO week, with IYYY being the corresponding year. So 5 weeks ago, which
was Dec 31st 2018, was ISO week 1 of 2019, but in some other week
numbering systems, it was week 53 of 2018.

-- 
Andrew (irc:RhodiumToad)




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: to_char function returning wrong data
@ 2019-02-04 17:31  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Sreeni Survi <sreenisurvi@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2019-02-04 17:31 UTC (permalink / raw)
  To: Sreeni Survi <sreenisurvi@gmail.com>; +Cc: pgsql-bugs

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




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2019-02-04 17:31 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-02-04 17:16 to_char function returning wrong data Sreeni Survi <sreenisurvi@gmail.com>
2019-02-04 17:25 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-02-04 17:31 ` Tom Lane <tgl@sss.pgh.pa.us>

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