Sunday, April 29, 2012

SQL Drop Time in DateTime


Problem
i have a problem where my table stores the date as
2009-01-10 10:00:00.000
and I have another table which stores the date as
2009-01-10 12:00:00.000
I know they are not the same but the dates are, is there away in SQL to easily drop the time and keep just the dates for comparison?

Solution:
Run this
SELECT DATEADD(dd, DATEDIFF(d, 0, Getdate()), 0)
change Getdate() to your column name to strip the date
BTW if you are doing a comparison it is better to do >= and < since it will perform better

No comments: