I am returning rows based on a date field equal to a datetime . They obviously correspond directly only in the format dd/MM/yyyy = dd/MM/yyyy 00:00:00 , but I look to ignore the time.
There are three methods that I have tried, they all work, but I wonder what is better.
1 - CONVERT(varchar(10),MyDate,103) = CONVERT(varchar(10),MyDateTime,103))
2 - MyDate = CONVERT(date,MyDateTime)
3 - MyDate = CAST(MyDateTime AS date)
4 - MyDate = DATEADD(dd, DATEDIFF(dd, 0, MyDateTime), 0)
For me, # 1 should be the slowest conversion to string, then using string comparisons must be the least efficient. But in tests it is the fastest! Below are my tests:
1 - 303 ms average
2 - 284 ms average
3 - 273 ms average
4 - 1745 ms average
Testing is performed with a sample size of ~ 300,000
Is there a reason for this? Is the first option really the best option?
EDIT: modified test values ββto display tests performed 10 times for records of 300 thousand records. Changes the result to show everything very similar except Tim Schmelter's DATEADD/DATEDIFF method, mentioned below. This is apparently the least effective.