Show datediff as seconds, milliseconds

datetime, sql, sql-server, t-sql

Solution

SELECT 
  DATEDIFF(MILLISECOND, begin, end) / 1000, 
  DATEDIFF(MILLISECOND, begin, end) % 1000
FROM ...;

If you absolutely must form it as a string in your SQL query (can't your presentation tier do that?), then:

SELECT 
  CONVERT(VARCHAR(12),  DATEDIFF(MILLISECOND, begin, end) / 1000)
  + ',' 
  + RIGHT('000' + CONVERT(VARCHAR(4), DATEDIFF(MILLISECOND, begin, end) % 1000), 3)
FROM ...;

Also I really hope you have better column names than `begin` and `end`.

Problem

I'm trying to calculate the difference between two datetime values. I tried `datediff(s, begin,end)` and `datediff(ms, begin,end)` however I want the difference to be returned as `seconds,milliseconds` like the following: ``` 4,14 63,54 ```

Original source