From: | Michael Glaesemann <grzm(at)seespotcode(dot)net> |
---|---|
To: | emilu(at)encs(dot)concordia(dot)ca |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Date for a week day of a month |
Date: | 2007-07-03 20:23:29 |
Message-ID: | 6C8FF22C-DDF2-47BE-9F9C-3BDA742E00E6@seespotcode.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Jul 3, 2007, at 14:54 , Emi Lu wrote:
> result := (
> (date_part('year', $1) || '-' || date_part('month',
> $1) || '-01')::date
> + '1 month'::interval - '1 day'::interval
> )::date;
I recommend not using string manipulation to handle data that is not
textual. There are a lot of date and time functions available. The
above can be rewritten in a couple of different ways:
result := (date_trunc('month', $1) + interval '1 month' - interval '1
day')::date;
result := (date_trunc('month', $1 + interval '1 month'))::date - 1;
For example:
SELECT current_date
, (date_trunc('month', current_date) + interval '1 month' -
interval '1 day')::date as all_intervals
, (date_trunc('month', current_date + interval '1 month'))::date
- 1 as date_arithmetic;
date | all_intervals | date_arithmetic
------------+---------------+-----------------
2007-07-03 | 2007-07-31 | 2007-07-31
(1 row)
Hope this helps.
Michael Glaesemann
grzm seespotcode net
From | Date | Subject | |
---|---|---|---|
Next Message | Richard Huxton | 2007-07-03 20:39:26 | Re: Stored Procedure: Copy table from; path = text variable |
Previous Message | Nick Barr | 2007-07-03 20:03:55 | Re: Date for a week day of a month |