Skip to content
Advertisement

Where condition with a specific hour and minute from a Timestamp SQL

I have to search for a particular hour and minute within a column of a table.

For example:

activitydate (timestamp without time zone)
---------------------
"2021-02-11 00:00:00"
"2021-02-01 00:00:00"
"2021-02-03 03:00:00"
"2021-02-04 00:00:00"
"2021-02-05 05:00:00"
"2021-02-05 00:00:00"
"2021-02-08 07:00:00"

I wanted to run a query like this:

SELECT *
FROM table_name
WHERE activitydate_hour = '03: 00 '

Is there a way to do it?

Advertisement

Answer

In standard SQL, you would use:

where extract(hour from activitydate) = 3

However, not all databases support extract(), so your database might have bespoke syntax.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement