Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL convert time to milliseconds

Tags:

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.

like image 520
Lingo Avatar asked Sep 16 '15 07:09

Lingo


People also ask

How do I get milliseconds from SQL query?

We can use DATEPART() function to get the MILLISECOND part of the DateTime in Sql Server, here we need to specify datepart parameter of the DATEPART function as millisecond or mi .

How do I get Currentdate in SQL?

MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric).


2 Answers

Use DATEDIFF:

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

Result:

23323 
like image 82
t-clausen.dk Avatar answered Sep 27 '22 21:09

t-clausen.dk


You can use datepart function. like this

select DATEPART(MILLISECOND,GETDATE())+DATEPART(second,getdate())*1000 
like image 36
Mukesh Kalgude Avatar answered Sep 27 '22 20:09

Mukesh Kalgude