- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the time difference and convert it to hours in MySQL?
You can achieve with the help of timestampdiff() method from MySQL. The syntax is as follows −
Syntax
SELECT ABS(TIMESTAMPDIFF(HOUR,yourColumnName1,yourColumnName2)) as anyVariableName from yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table DifferenceInHours -> ( -> StartDateTime datetime, -> EndDateTime datetime -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into DifferenceInHours values('2018-12-20 10:00:00', '2018-12-19 12:00:00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DifferenceInHours values('2018-12-20 11:00:00', '2018-12-19 11:00:00'); Query OK, 1 row affected (0.16 sec) mysql> insert into DifferenceInHours values('2018-12-20 10:30:00', '2018-12-19 11:00:00'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from DifferenceInHours;
Output
+---------------------+---------------------+ | StartDateTime | EndDateTime | +---------------------+---------------------+ | 2018-12-20 10:00:00 | 2018-12-19 12:00:00 | | 2018-12-20 11:00:00 | 2018-12-19 11:00:00 | | 2018-12-20 10:30:00 | 2018-12-19 11:00:00 | +---------------------+---------------------+ 3 rows in set (0.00 sec)
Here is the query that gets time difference in hours. The query is as follows −
mysql> SELECT ABS(TIMESTAMPDIFF(HOUR,StartDateTime,EndDateTime)) as Hour from DifferenceInHours;
The following is the output displaying the time difference in hours −
+------+ | Hour | +------+ | 22 | | 24 | | 23 | +------+ 3 rows in set (0.00 sec)
- Related Articles
- How to convert a MySQL TIME value to days and hours form?
- Select current time with MySQL now() and convert it to GMT 0?
- Add 2 hours to current time in MySQL?
- MySQL query to check how to get time difference
- How to add 5 hours to current time in MySQL?
- Get the time difference between values in different columns with MySQL
- How to convert varchar “time” to real time in MySQL?
- Get field value and convert a particular character from it to uppercase with MySQL
- Convert:3 hours and 12 minutes into hours.
- How to convert string to time in MySQL?
- Convert MySQL time from HH:MM:SS to HH:MM
- Convert number INT in minutes to TIME in MySQL?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- JavaScript program to convert 24 hours format to 12 hours
- Convert datetime to get month name in MySQL?

Advertisements