Re: Where condition doesn't work as expected

From: Bzzzz <lazyvirus(at)gmx(dot)com>
To: William Colls <william(at)williamcollsassoc(dot)ca>
Cc: pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Re: Where condition doesn't work as expected
Date: 2020-02-08 23:18:55
Message-ID: 20200208231855.47b400fa@msi.defcon1.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Sat, 8 Feb 2020 17:39:32 -0500
William Colls <william(at)williamcollsassoc(dot)ca> wrote:

> I have the following condition a select statement:
>
>  WHERE "datetime" > '2019-03-31 23:59:59'
>
> datetime is a character field with data in the format YYYY-MM-DD
> HH:MM:SS. However it returns any line where the datetime field begins
> with 2019. I suspect that I should be using timestamp values, but I
> can't figure out how to cast the datetime field value to a timestamp.

Depending on what you need, use either TIMESTAMP or TIMESTAMPTZ.

[…] WHERE (datetime::TIMESTAMP) > TIMESTAMP'2019-03-31 23:59:59' ;

(note that the TIMESTAMP to the right of the comparison is not
mandatory.)

As it costs, if you have no special reason for the "datetime" column to
dwell in text or varchar, you should consider converting it to a
TIMESTAMP or TIMESTAMPTZ type to avoid any conversion when querying.

Jean-Yves

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Gavin Flower 2020-02-10 09:49:10 Re: Password aging
Previous Message Ken Benson 2020-02-08 22:43:46 RE: Where condition doesn't work as expected