Lingo
Lingo

Reputation: 600

SQL convert time to milliseconds

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.

Upvotes: 24

Views: 60549

Answers (3)

Lingo
Lingo

Reputation: 600

I got it, it isn't the nice way but it works:

SELECT (DATEPART(hh,'00:00:23.323') * 60 * 60 * 1000) + (DATEPART(n,'00:00:23.323') * 60 * 1000) + (DATEPART(s,'00:00:23.323') * 1000) + DATEPART(ms,'00:00:23.323') AS 'DurationInMillis'

Upvotes: 2

t-clausen.dk
t-clausen.dk

Reputation: 44316

Use DATEDIFF:

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

Result:

23323

Upvotes: 36

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

You can use datepart function. like this

select DATEPART(MILLISECOND,GETDATE())+DATEPART(second,getdate())*1000

Upvotes: 3

Related Questions