I am trying to select data from a table, using a "like" on date field "date_checked" (timestamp). But I have this error :
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: timestamp without time zone
My request is :
SELECT my_table.id FROM my_table WHERE my_table.date_checker LIKE '2011-01-%'
I don't want to use :
SELECT my_table.id FROM my_table WHERE my_table.date_checker >= '2011-01-01 00:00:00' AND my_table.date_checker < '2011-02-01 00:00:00'
PostgreSQL timestamp example First, create a table that consists of both timestamp the timestamptz columns. Next, set the time zone of the database server to America/Los_Angeles . After that, query data from the timestamp and timestamptz columns. The query returns the same timestamp values as the inserted values.
Like operator will not work with DateTime.
To calculate the difference between the timestamps in PostgreSQL, simply subtract the start timestamp from the end timestamp. Here, it would be arrival - departure . The difference will be of the type interval , which means you'll see it in days, hours, minutes, and seconds.
The PostgreSQL LIKE operator is used to match text values against a pattern using wildcards. If the search expression can be matched to the pattern expression, the LIKE operator will return true, which is 1. The percent sign represents zero, one, or multiple numbers or characters.
It's all very well not "wanting to use" < and > with timestamps, but those operators can be converted into index scans, and a string-match... well, it can, but EWWWW.
Well, the error is occurring because you need to explicitly convert the timestamp to a string before using a string operation on it, e.g.:
date_checker::text LIKE '2011-01-%'
and I suppose you could then create an index on (date_checker::text)
and that expression would become an index scan but.... EWWWW.
Try this:
SELECT my_table.id FROM my_table WHERE CAST(my_table.date_checker AS VARCHAR) LIKE '2011-01-%';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With