SQL convert time to milliseconds

Lingo picture Lingo · Sep 16, 2015 · Viewed 36.8k times · Source

I have this time-duration: 00:00:23.323 I want to convert it in sql to milliseconds.

EDIT:// I tried this but it isn't very nice:

SELECT  (DATEPART(hh,'12:13:14.123') * 60 * 60 * 1000)
SELECT  (DATEPART(n,'12:13:14.123') * 60 * 1000)
SELECT  (DATEPART(s,'12:13:14.123') * 1000)
SELECT  DATEPART(ms,'12:13:14.123')

How does it work?

Thanks for your answers.

Answer

t-clausen.dk picture t-clausen.dk · Sep 16, 2015

Use DATEDIFF:

SELECT DATEDIFF(MILLISECOND, 0, '00:00:23.323')

Result:

23323