R.J. Robinson
R.J. Robinson

Reputation: 2240

SQL query by block of time

I am trying to pull a report from my DB ( mySQL ).

From 9am to 9pm.

By schema has the dates in DATETIME ( MM-DD-YYYY HH:MM:SS )

``` SQL

SELECT COUNT(*) FROM table_name
WHERE date_created BETWEEN '09:00:00' AND '21:00:00'

thoughts? Do I have to cast the DATETIME into something else?

Upvotes: 1

Views: 41

Answers (1)

Jim
Jim

Reputation: 3508

You need to just get the hours:

SELECT COUNT(*) FROM table_name
WHERE DATEPART(HOUR, date_created) BETWEEN 9 AND 21

Upvotes: 4

Related Questions