 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL convert timediff output to day, hour, minute, second format?
To understand the MySQL convert timediff output to day, hour, minute, and second format, you need to use CONCAT() from MySQL.
Let us create a table. The query to create a table is as follows:
mysql> create table convertTimeDifferenceDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> StartDate datetime, -> EndDate datetime, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command. The query to insert record is as follows:
mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values(date_add(now(),interval -3 hour),date_add(now(),interval 3 hour)); Query OK, 1 row affected (0.41 sec) mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values(date_add(now(),interval -2 hour),date_add(now(),interval 2 hour)); Query OK, 1 row affected (0.27 sec) mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values('2018-04-05 12:30:35','2018-05-17 14:30:50'); Query OK, 1 row affected (0.14 sec) mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values('2017-10-11 11:20:30','2017-12-17 15:21:55'); Query OK, 1 row affected (0.20 sec) Display all records from the table using select statement. The query is as follows:
mysql> select *from convertTimeDifferenceDemo;
The following is the output:
+----+---------------------+---------------------+ | Id | StartDate | EndDate | +----+---------------------+---------------------+ | 1 | 2019-01-28 20:55:33 | 2019-01-29 02:55:33 | | 2 | 2019-01-28 21:57:42 | 2019-01-29 01:57:42 | | 3 | 2018-04-05 12:30:35 | 2018-05-17 14:30:50 | | 4 | 2017-10-11 11:20:30 | 2017-12-17 15:21:55 | +----+---------------------+---------------------+ 4 rows in set (0.00 sec)
Here is the query to get timediff output to day, hour, minute, and second format:
mysql> SELECT CONCAT( -> FLOOR(HOUR(TIMEDIFF(StartDate,EndDate)) / 24), ' DAYS ', -> MOD(HOUR(TIMEDIFF(StartDate,EndDate)), 24), ' HOURS ', -> MINUTE(TIMEDIFF(StartDate,EndDate)), ' MINUTES ') AS DESCRIPTION -> FROM convertTimeDifferenceDemo;
The following is the output:
+------------------------------+ | DESCRIPTION | +------------------------------+ | 0 DAYS 6 HOURS 0 MINUTES | | 0 DAYS 4 HOURS 0 MINUTES | | 34 DAYS 22 HOURS 59 MINUTES | | 34 DAYS 22 HOURS 59 MINUTES | +------------------------------+ 4 rows in set, 6 warnings (0.04 sec)
Advertisements
 