Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter/WHERE using timestamp

I am trying to create a filter to see anything happening during a certain period of time of day using my datetime variable which has the date and time in format like this:

2009-07-30 16:05:15

How I can create a WHERE clause that will let me look at any entries where the timestamp of that variable is between certain hours such as 17:00:00 and 07:00:00 the following day.

Which Imagine would be two separate clauses starting

> 17:00:00 OR < 07:00:00 respectively

like image 714
OM Asphyxiate Avatar asked Dec 01 '25 09:12

OM Asphyxiate


1 Answers

Use DATEPART function

WHERE DATEPART(hh, [Date Column]) between 7 and 17

or

WHERE NOT DATEPART(hh, [Date Column]) between 7 and 17

for the night

like image 145
xdd Avatar answered Dec 04 '25 00:12

xdd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!