In MS SQL 2000 and 2005, given a datetime such as '2008-09-25 12:34:56' what is the most efficient way to get a datetime containing only '2008-09-25'?
Duplicated here.
In MS SQL 2000 and 2005, given a datetime such as '2008-09-25 12:34:56' what is the most efficient way to get a datetime containing only '2008-09-25'?
Duplicated here.
CONVERT, FLOOR ,and DATEDIFF will perform just the same.
How to return the date part only from a SQL Server datetime datatype
in SQL server 2012 use
DateDiff(Day, 0, GetDate()) is the same as DateDiff(Day, '1900-01-01', GetDate())
Since DateDiff returns an integer, you will get the number of days that have elapsed since Jan 1, 1900. You then add that integer number of days to Jan 1, 1900. The net effect is removing the time component.
I should also mention that this method works for any date/time part (like year, quarter, month, day, hour, minute, and second).
The last one, for seconds, requires special handling. If you use Jan 1, 1900 you will get an error.
Difference of two datetime columns caused overflow at runtime.
You can circumvent this error by using a different reference date (like Jan 1, 2000).
To get YYYY-MM-DD, use:
Edit: Oops, he wants a DateTime instead of a string. The equivalent of TRUNC() in Oracle. You can take what I posted and cast back to a DateTime: