Usage of MySQL time difference functions TIMESTAMPDIFF and DATEDIFF

Usage of MySQL time difference functions TIMESTAMPDIFF and DATEDIFF

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:
  • MySQL time difference functions (TIMESTAMPDIFF, DATEDIFF), date conversion calculation functions (date_add, day, date_format, str_to_date)
  • Practical MySQL + PostgreSQL batch insert update insertOrUpdate
  • Detailed usage of MYSQL row_number() and over() functions
  • Solution to index failure caused by MySQL implicit type conversion
  • A brief discussion on whether too many MySQL data queries will cause OOM
  • Django builds MySQL master-slave to achieve read-write separation
  • Detailed explanation of MySQL multi-table join query
  • Detailed explanation of TIMESTAMPDIFF case in MySQL

<<:  Detailed explanation of how to copy and backup docker container data

>>:  Vue custom optional time calendar component

Recommend

Ajax jquery realizes the refresh effect of a div on the page

The original code is this: <div class='con...

Example code comparing different syntax formats of vue3

The default template method is similar to vue2, u...

How to start multiple MySQL instances in CentOS 7.0 (mysql-5.7.21)

Configuration Instructions Linux system: CentOS-7...

Summary of MySQL injection bypass filtering techniques

First, let’s look at the GIF operation: Case 1: S...

Common ways to optimize Docker image size

The Docker images we usually build are usually la...

Example analysis of MySQL startup and connection methods

Table of contents How to start mysqld Method 1: m...

How to use MySQL DATEDIFF function to get the time interval between two dates

describe Returns the time interval between two da...

A brief discussion on tags in HTML

0. What is a tag? XML/HTML CodeCopy content to cl...

Do you know how to use mock in vue project?

Table of contents first step: The second step is ...

Idea deployment tomcat service implementation process diagram

First configure the project artifacts Configuring...

Vue implements tab navigation bar and supports left and right sliding function

This article mainly introduces: using Vue to impl...

Example code of vue custom component to implement v-model two-way binding data

In the project, you will encounter custom public ...

Detailed steps to store emoji expressions in MySQL

Caused by: java.sql.SQLException: Incorrect strin...