pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Ken Tanzer <ken.tanzer@gmail.com>
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Immutable way to cast timestamp TEXT to DATE? (for index)
Date: Fri, 04 Jan 2019 22:54:16 +0000
Message-ID: <875zv4rquz.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAD3a31XupySutj+RGLxQrOqji5gBZo6Z05rAYgRDRH10wi4Mag@mail.gmail.com>
References: <CAD3a31XupySutj+RGLxQrOqji5gBZo6Z05rAYgRDRH10wi4Mag@mail.gmail.com>

>>>>> "Ken" == Ken Tanzer <ken.tanzer@gmail.com> writes:

 Ken> Hi. I've got a text field in a table that holds this style of
 Ken> timestamp:

 Ken> 2014-10-23T00:00:00

You can't make this a field of type "timestamp" rather than text?

Casts from text to either date or timestamp are mutable because they
depend on the current DateStyle value (_you_ might know that your values
are always ISO format, but the code doesn't). You can't even avoid this
with to_date or to_timestamp and a fixed format, because those functions
are also mutable since some of the format options are locale-dependent
(again, you might know that you're not using those, but the code
doesn't).

If the column type were timestamp, though, then you could do comparisons
directly, or cast the value to "date" (which is an immutable cast).

If you absolutely can't change the column type, then one option would be
to do your own fixed-format date parsing function (and label it
immutable), e.g.

create function iso_timestamp(text)
  returns timestamp without time zone
  as $$ select case when $1 ~ '^\d\d\d\d-?\d\d-?\d\dT\d\d:?\d\d:?\d\d$'
                    then $1::timestamp
                    else null end $$
  set DateStyle = 'ISO,YMD'
  language sql immutable strict;

or

create function iso_date(text)
  returns date
  as $$ select case when $1 ~ '^\d\d\d\d-?\d\d-?\d\d(?![^T])'
                    then substring($1 from '^\d\d\d\d-?\d\d-?\d\d')::date
                    else null end $$
  set DateStyle = 'ISO,YMD'
  language sql immutable strict;

-- 
Andrew (irc:RhodiumToad)




view thread (7+ messages)  latest in thread

Message-ID: <875zv4rquz.fsf@news-spur.riddles.org.uk>
Permalink:  ../875zv4rquz.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/875zv4rquz.fsf@news-spur.riddles.org.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, ken.tanzer@gmail.com
  Subject: Re: Immutable way to cast timestamp TEXT to DATE? (for index)
  In-Reply-To: <875zv4rquz.fsf@news-spur.riddles.org.uk>
  References: <CAD3a31XupySutj+RGLxQrOqji5gBZo6Z05rAYgRDRH10wi4Mag@mail.gmail.com> <875zv4rquz.fsf@news-spur.riddles.org.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox