Reputation: 361
I have a MySQL table called event to record the whole month data, and it has one column called EventTime to record the time each of them been written into database.
Could I query the data in specific time range, regardless of their date? For Instance, I want to query the data between 12:00 and 18:00, then it could return all data which occur between the time range in the whole month?
I believe I could do it with a for-loop in my C# code, but I'd like to know is there any SQL command I could use?
Upvotes: 0
Views: 559
Reputation: 1812
you can use Time Function here is complete detail of Date and Time Functions
-- it will pull all records within current Month i.e December Now and given Time Range.
Select Time(EventTime) from event where MONTH(EventTime) = Month(Current_Date) and time(EventTime) BETWEEN '12:00' and '18:00'
Upvotes: 0
Reputation: 919
You cast the column to TIME and query it. example:
select time(event_time) from events where time(event_time) BETWEEN '10:00' and '11:00'
the downside of this approach is that the DB won't use any index on event_time (unless you created a function based index on time(event_time))
Upvotes: 1