Re: Date calculation

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Ron <ronljohnsonjr(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Date calculation
Date: 2019-01-31 21:00:48
Message-ID: 875zu4a6ox.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>>>>> "Bruce" == Bruce Momjian <bruce(at)momjian(dot)us> writes:

Bruce> Oh, right, you want date, so use:

Bruce> SELECT date_trunc('week', CURRENT_DATE) + '6 days';

Three major things wrong with this:

1. If you do this on Sunday, it gives you the current day not the _next_
Sunday.

2. If you try and do this for other days of the week it doesn't work at
all, instead giving you the specified day of the current week whether or
not it's before or after the current day.

3. It's letting PG cast the date to a timestamptz, which is inefficient,
possibly incorrect, and mutable; you want to force it to cast to
timestamp without timezone instead. (A good rule of thumb is that you
virtually never want to cast dates to timestamptz; the natural cast from
date is to timestamp _without_ timezone.)

--
Andrew (irc:RhodiumToad)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Moreno Andreo 2019-02-01 13:41:51 Re: problem
Previous Message Andrew Gierth 2019-01-31 20:53:07 Re: Date calculation