Alanight
Alanight

Reputation: 361

How to query data between specific time, regardless of their date?

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

Answers (2)

sm.abdullah
sm.abdullah

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

asafm
asafm

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

Related Questions