I am making a DATEDIFF() call in SQL server 2008 as part of a stored procedure, which potentially is returning values of as small as 3 seconds or less. I would like the format to be in MM:SS (i.e. 00:03 when 3 seconds). I was originally using the call in minutes:
DATEDIFF(mi, SELECT MAX(startDate) FROM myTable , SELECT MAX(EndDate) FROM myTable)
However, this was rounding down to the nearest minute, and thus erasing the seconds values. How can I achieve a format as specified above using DATEDIFF?
To the nearest second:
Ref.
Note:
DATEDIFFwon’t give you a ‘format’. It just returns an integer. So you’ll need to cast/format appropriately.I suspect the closest you will get easily is using
CASTwith format108:e.g.
returns:
[Note: If you want to round to the nearest 3 seconds, you could modify this: TSQL: Round to Nearest 15 Minute Interval ]