Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to compare a datetime to midnight in SQL

Tags:

sql

sql-server

I want to select from a SQL Server table where a datetime field is > than midnight of the current date.

I'm using the following but would like to ask if there is a more efficient way to do it?

 WHERE start_time > (Select DATEADD(d,0,DATEDIFF(d,0,GETDATE())))
like image 659
DarkW1nter Avatar asked Sep 11 '25 08:09

DarkW1nter


1 Answers

If you convert or cast the current date to a date instead of datetime, I believe you can use that in your WHERE because the date data type is set to midnight:

WHERE start_time > CONVERT(date, GETDATE())

OR

WHERE start_time > CAST(GETDATE() AS date)
like image 141
K Richard Avatar answered Sep 12 '25 20:09

K Richard