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 17:24:05
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 17:24:05
This will truncate the milliseconds.
or
CAST and CONVERT
DATEADD
DATEPART
The following has very fast performance, but it not only removes millisecond but also rounds to minute. See (http://msdn.microsoft.com/en-us/library/bb677243.aspx)
Edit:
The following script is made to compare the scripts from Mikael and gbn I upvoted them both since both answers are great. The test will show that gbn' script is slightly faster than Mikaels:
First run
Second run
Third run
so, the easiest way now is:
select convert(datetime2(0) , getdate())
The fastest, also language safe and deterministic