What is the best way to shorten a datetime that includes milliseconds to only have the second?
For example 2012-01-25 17:24:05.784
to 2012-01-25
Expanding on accepted answer by @Mikael Eriksson:
To truncate a datetime2(7) to 3 places (aka milliseconds):
-- Strip of fractional part then add desired part back in
select dateadd(nanosecond,
-datepart(nanosecond, TimeUtc) + datepart(millisecond, TimeUtc) * 1e6,
TimeUtc) as TimeUtc
The current max precision of datetime2(p) is (7) (from docs.microsoft.com)
convert(datetime, convert(varchar, @datetime_var, 120), 120)