Show datediff as seconds, milliseconds

后端 未结 3 1075
面向向阳花
面向向阳花 2021-01-01 15:46

I\'m trying to calculate the difference between two datetime values.

I tried datediff(s, begin,end) and datediff(ms, begin,end) however I w

3条回答
  •  孤独总比滥情好
    2021-01-01 15:57

    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.

提交回复
热议问题