Usage of time difference functions TIMESTAMPDIFF and DATEDIFF When we write SQL statements, especially stored procedures, we frequently use comparisons and judgments on dates and times. Here is an example of how to use these two time difference comparison functions. The datediff function returns the difference in days and cannot be localized to hours, minutes, and seconds. -- 2 days difference select datediff('2018-03-22 09:00:00', '2018-03-20 07:00:00'); The TIMESTAMPDIFF function has parameter settings and can be accurate to days (DAY), hours (HOUR), minutes (MINUTE), and seconds (SECOND). It is more flexible to use than the datediff function. When comparing two times, put the smaller one in front and the larger one in the back. --Difference of 1 day select TIMESTAMPDIFF(DAY, '2018-03-20 23:59:00', '2015-03-22 00:00:00'); --The difference is 49 hours select TIMESTAMPDIFF(HOUR, '2018-03-20 09:00:00', '2018-03-22 10:00:00'); --The difference is 2940 minutes select TIMESTAMPDIFF(MINUTE, '2018-03-20 09:00:00', '2018-03-22 10:00:00'); --The difference is 176400 seconds select TIMESTAMPDIFF(SECOND, '2018-03-20 09:00:00', '2018-03-22 10:00:00'); How to write in a stored procedure: Note: var_committime is the time required for comparison obtained in the previous business. -- Get the current time SET var_current_time = CONCAT(CURDATE(),' ',CURTIME()); -- Time comparison SET var_time_diff = TIMESTAMPDIFF(MINUTE, var_committime, var_current_time); -- Determine whether the unreviewed contract has not been processed for more than 48 hours. If so, perform subsequent logical processing, otherwise do not process it. IF (var_time_diff > 2880) THEN -- Related business logic processing END IF; The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of how to copy and backup docker container data
>>: Vue custom optional time calendar component
Table of contents 1. Introduction 2. Back up the ...
You can save this logo locally as a .rar file and...
Table of contents Stabilization Throttling: Anti-...
On a Linux computer, there are two times, one is ...
1. Problem The problems encountered when initiali...
I searched a lot of articles online but didn'...
01. Command Overview Linux provides a rich help m...
Table of contents Principle Source code analysis ...
I installed MySQL smoothly in Ubuntu 16.04 before...
Table of contents Object parameters using destruc...
I'm very happy. When encountering this proble...
Today I will introduce to you a difference betwee...
1. The role of index Generally speaking, an index...
Table of contents 1. Install dependencies 2. Conf...
Table of contents 1. Interface effect preview 2.u...