dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Intermediate
May 5, 2020

Converting TIME to SECONDS in MySQL

There are programming instances in which we would need a given time to be converted to seconds to be used for certain operations.

MySQL provides an inbuilt function named TIME_TO_SEC, that accepts time as a parameter. The returned value is in seconds, computed from 00:00:00.

Consider the SQL query below to understand further.

SELECT TIME_TO_SEC('00:1:12'); 

This is 1 minute and 12 seconds which when converted to seconds is 60+12=72 seconds.

Output:

+-------------------------+
| TIME_TO_SEC('00:1:12')  |
+-------------------------+
|                     72  |
+-------------------------+

Now, consider the second example.

SELECT TIME_TO_SEC('23:59:59');

Output:

+-------------------------+
| TIME_TO_SEC('23:59:59') |
+-------------------------+
|                  86,399 |
+-------------------------+
M S Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date