Lists: | pgsql-general |
---|
From: | r d <rd0002(at)gmail(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Question about PARTIAL DATE type/s |
Date: | 2012-10-07 11:28:24 |
Message-ID: | CALtFtEK35HqMr34hKyyCTV2g522fpiJK+6qAS6rZVdeTWgWS5g@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Hi,
I have text fields which contains dates in the format 'YYYYMM' (four
positions for the year, two for the month).
These fields are contained in text files which I load into the DB.
When I convert this field to a date using
to_date("PARTIAL_DATE_FIELD",'YYYYMM'); -- (a cast won't recognize
the input as valid)
I still get full dates as an output, for example,
'198801' ---> 1988-01*-01*
'196408' ---> 1964-08*-01*
and so on, what is wrong in this case because nowhere it is said that I
have the /first/ of that month, the entry just says that the event in
question happened /during/ that month.
*My question:*
Is it possible to define fields which contain partial dates per above? I
found nothing in the manual.
Thanks,
RD
From: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
---|---|
To: | r d <rd0002(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Question about PARTIAL DATE type/s |
Date: | 2012-10-07 11:59:14 |
Message-ID: | CA+mi_8a+6=ZMA-XxYDZvrsfRjnyCh9zE5-MJmT708DNEBYzC=A@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
On Sun, Oct 7, 2012 at 12:28 PM, r d <rd0002(at)gmail(dot)com> wrote:
> Hi,
>
> I have text fields which contains dates in the format 'YYYYMM' (four
> positions for the year, two for the month).
> These fields are contained in text files which I load into the DB.
>
> When I convert this field to a date using
>
> to_date("PARTIAL_DATE_FIELD",'YYYYMM'); -- (a cast won't recognize
> the input as valid)
>
> I still get full dates as an output, for example,
> '198801' ---> 1988-01-01
> '196408' ---> 1964-08-01
> and so on, what is wrong in this case because nowhere it is said that I have
> the /first/ of that month, the entry just says that the event in question
> happened /during/ that month.
>
>
> My question:
> Is it possible to define fields which contain partial dates per above? I
> found nothing in the manual.
There is no "partial date" type. You can use a dates range to
represent explicitly what you mean.
http://www.postgresql.org/docs/9.2/static/rangetypes.html
e.g. this function returns the range requested:
postgres=# create function partial_month(s text)
returns daterange
language sql
as $$
select daterange(
to_date($1, 'YYYYMM'),
to_date(($1::int + 1)::text, 'YYYYMM'),
'[)');
$$;
CREATE FUNCTION
postgres=# select partial_month('201202');
partial_month
-------------------------
[2012-02-01,2012-03-01)
(1 row)
postgres=# select partial_month('201212');
partial_month
-------------------------
[2012-12-01,2013-01-01)
(1 row)
Note: it exploits to_date() parsing '200013' as '2001-01', which is
reasonable but haven't found documented and don't know how much
reliable. Writing a safer "one month later" function is left as
exercise.
-- Daniele
From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
Cc: | r d <rd0002(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: Question about PARTIAL DATE type/s |
Date: | 2012-10-07 14:03:08 |
Message-ID: | 24996.1349618588@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> writes:
> Note: it exploits to_date() parsing '200013' as '2001-01', which is
> reasonable but haven't found documented and don't know how much
> reliable. Writing a safer "one month later" function is left as
> exercise.
Consider adding '1 month'::interval to the month start date.
(This function relies on text-munging way too much for my taste.
There's almost always a better way to do it than that.)
regards, tom lane
From: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | r d <rd0002(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: Question about PARTIAL DATE type/s |
Date: | 2012-10-07 14:46:14 |
Message-ID: | CA+mi_8bQnReqWBacU-xA3SEgwHWy=P60nvjKnYFXjH+XerMmnA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
On Sun, Oct 7, 2012 at 3:03 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> writes:
>> Note: it exploits to_date() parsing '200013' as '2001-01', which is
>> reasonable but haven't found documented and don't know how much
>> reliable. Writing a safer "one month later" function is left as
>> exercise.
>
> Consider adding '1 month'::interval to the month start date.
>
> (This function relies on text-munging way too much for my taste.
> There's almost always a better way to do it than that.)
Didn't realize intervals store months/days info separately: I thought
an interval was just a vector in the timestamp space. Nice surprise.
-- Daniele
From: | r d <rd0002(at)gmail(dot)com> |
---|---|
To: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
Cc: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: Question about PARTIAL DATE type/s |
Date: | 2012-10-07 21:12:53 |
Message-ID: | CALtFtE+3+469HtdJcWQTGiXcNzONG+B91ne4wXy0Wsg71JKcTQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
I suspected that this would be the answer.
Thank you lots for your kind help, Daniele & Tom [?]
On 7 October 2012 16:46, Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>wrote:
> On Sun, Oct 7, 2012 at 3:03 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> writes:
> >> Note: it exploits to_date() parsing '200013' as '2001-01', which is
> >> reasonable but haven't found documented and don't know how much
> >> reliable. Writing a safer "one month later" function is left as
> >> exercise.
> >
> > Consider adding '1 month'::interval to the month start date.
> >
> > (This function relies on text-munging way too much for my taste.
> > There's almost always a better way to do it than that.)
>
> Didn't realize intervals store months/days info separately: I thought
> an interval was just a vector in the timestamp space. Nice surprise.
>
> -- Daniele
>