0

I have a syntax, but it doesn't work.

Here is my query:

SELECT *
FROM aqua.reading
WHERE
  CAST(reading.pres_date AS VARCHAR)
  BETWEEN LIKE '2022-10-18%' AND LIKE '2022-10-18%'  

it says:

ERROR:  type "like" does not exist
LINE 1: ... WHERE CAST(reading.pres_date AS VARCHAR) BETWEEN LIKE '2022...
                                                             ^
SQL state: 42704
Character: 77

I am trying to get all the data with timestamp and timezone and implement a date range

6
  • 3
    There is no such syntax. LIKE is used for character strings, not dates, and there is no BETWEEN LIKE. You don't need it anyway. Remove the % from your two dates and just use BETWEEN. Commented Jan 11, 2023 at 1:38
  • You also don't need to CAST the dates. Just use BETWEEN on the date values. Commented Jan 11, 2023 at 1:45
  • 1
    This is the correct syntax: WHERE reading.pres_date BETWEEN '2022-10-18' AND '2022-10-18' Commented Jan 11, 2023 at 1:51
  • 2
    Additional note: using BETWEEN .. AND .., when beginning and ending dates are the same, makes no sense. Commented Jan 11, 2023 at 1:53
  • 1
    ... WHERE reading.pres_date = '2022-10-18'. Assuming reading.pres_date is actually a date type value. Commented Jan 11, 2023 at 5:21

1 Answer 1

1

Don't compare dates (or timestamps) as strings. Compare them to proper date (or timestamp) values. Given the fact that you use the same "date" but with a wildcard at the end, I am assuming(!) that pres_date is in fact a timestamp column and you want to find all rows with a given date regardless of the time value of the timestamp.

The best approach is to use a range query with >= ("greater than or equal) on the lower value and < (strictly lower than) on the next day:

SELECT *
FROM aqua.reading
WHERE reading.pres_date >= DATE '2022-10-18' 
  AND reading.pres_date < DATE '2022-10-19'

Alternatively you can cast the timestamp to a date and use the = operator if you really want to pick just one day:

SELECT *
FROM aqua.reading
WHERE cast(reading.pres_date as DATE) = DATE '2022-10-18' 

However that will not make use of a potential index on pres_date so is likely to be slower than the range query from the first solution

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.