Tech Dreams

A Better Way To Extract Date From DateTime Columns [SQL Server]

Quite a long ago I wrote about a SQL Server programming tip on how to extract date part from a DATETIME column. The post discusses about using of T SQL function convert() to get date part. One of the readers of the post, tipped me about a better way of extracting date part and here is the SQL query he sent to us

SELECT DateAdd(day, DateDiff(day, 0, getdate()), 0);

In clean way this query trims off time part from the DATETIME value. I rate this solution better than the one I wrote long ago as this one does not depend on any string operations. According the commenter, this method is faster compared to the other. What do you say?

Thanks Yamo