Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gqi6I-0006hN-5V for pgsql-bugs@arkaria.postgresql.org; Mon, 04 Feb 2019 17:32:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gqi6G-0006uT-PZ for pgsql-bugs@arkaria.postgresql.org; Mon, 04 Feb 2019 17:32:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gqi6G-0006tl-Io for pgsql-bugs@lists.postgresql.org; Mon, 04 Feb 2019 17:32:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gqi6E-0007hN-KA for pgsql-bugs@postgresql.org; Mon, 04 Feb 2019 17:32:04 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x14HVxQa008047; Mon, 4 Feb 2019 12:31:59 -0500 From: Tom Lane To: Sreeni Survi cc: pgsql-bugs@postgresql.org Subject: Re: to_char function returning wrong data In-reply-to: References: Comments: In-reply-to Sreeni Survi message dated "Mon, 04 Feb 2019 12:16:06 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8045.1549301519.1@sss.pgh.pa.us> Date: Mon, 04 Feb 2019 12:31:59 -0500 Message-ID: <8046.1549301519@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Sreeni Survi 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