MySQL query to convert timediff() to seconds?



For this, you can use TIME_TO_SEC() function. Let us first create a table −

mysql> create table DemoTable    -> (    -> SourceTime time,    -> DestinationTime time    -> ); Query OK, 0 rows affected (1.33 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('10:20:00','4:50:54'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('12:05:10','7:45:12'); Query OK, 1 row affected (0.30 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------------+-----------------+ | SourceTime | DestinationTime | +------------+-----------------+ | 10:20:00   | 04:50:54        | | 12:05:10   | 07:45:12       | +------------+-----------------+ 2 rows in set (0.00 sec)

Following is query to convert the time difference to seconds −

mysql> SELECT ABS(TIME_TO_SEC(timediff(DestinationTime, SourceTime))) from DemoTable;

Output

This will produce the following output −

+---------------------------------------------------------+ | ABS(TIME_TO_SEC(timediff(DestinationTime, SourceTime))) | +---------------------------------------------------------+ | 19746                                                   | | 15598                                                 | +---------------------------------------------------------+ 2 rows in set (0.00 sec)
Updated on: 2020-06-30T10:01:16+05:30

582 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements