0

I have a string field in a table whose value is date format like: 2020-12-08T18:06:55.132Z or 2020-12-08T18:06:55.132+11:00.

How can I search this field based on date? Like select * from my_table where timestamp > '2020-12-08T00:00:00'.

2
  • Both are valid timestamps with time zone, why do you use a string (text/char/varchar) in the first place? Commented Jun 7, 2021 at 9:24
  • because it can be in different date format Commented Jun 7, 2021 at 10:16

1 Answer 1

1

You can try something like:

SELECT *
FROM my_table
WHERE to_timestamp(timestamp,'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') > '2020-12-08T00:00:00'

EDIT:

Something even easier, which doesn't rely on your timestamp format:

SELECT *
FROM my_table
WHERE timestamp::timestamp > '2020-12-08T00:00:00'
Sign up to request clarification or add additional context in comments.

3 Comments

I got this error ERROR: value for "YYYY" in source string is out of range DETAIL: Value must be in the range -2147483648 to 2147483647.
I've edited my answer for using "to_timestamp" instead of "to_date". Concerning your error, do you have the entry causing the issue? Because when I try with your example date (by putting '2020-12-08T18:06:55.132Z' instead of timestamp, it works)
I've added to my answer another query which doesn't rely on a given format, it may works better.

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.