I am trying to convert an interval value to a string value using TO_CHAR()
function. But I noticed when I pass formatter for weeks, it always returns at least 01, whereas it works as expected for days and months. For example:
postgres=# select to_char('2023-07-02'::timestamp - '2023-07-02'::timestamp, 'DD') as period;
period
--------
00
(1 row)
postgres=# select to_char('2023-07-02'::timestamp - '2023-07-02'::timestamp, 'WW') as period;
period
--------
01
(1 row)
postgres=# select to_char('2023-07-02'::timestamp - '2023-07-02'::timestamp, 'MM') as period;
period
--------
00
(1 row)
I even tried passing an interval like this:
postgres=# select to_char(interval '0 days', 'WW') as period;
period
--------
01
(1 row)
postgres=# select to_char(interval '0 days', 'DD') as period;
period
--------
00
(1 row)
postgres=# select to_char(interval '0 days', 'MM') as period;
period
--------
00
(1 row)
Please help. Is there any other way to convert an interval to 'weeks' string reliably?