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

Super detailed teaching on how to upgrade the version of MySQL

Table of contents 1. Introduction 2. Back up the ...

How to hide rar files in pictures

You can save this logo locally as a .rar file and...

JavaScript deshaking and throttling examples

Table of contents Stabilization Throttling: Anti-...

How to modify the time zone and time in Ubuntu system

On a Linux computer, there are two times, one is ...

Detailed tutorial on using the tomcat8-maven-plugin plugin in Maven

I searched a lot of articles online but didn'...

Specific use of Linux man command

01. Command Overview Linux provides a rich help m...

React event mechanism source code analysis

Table of contents Principle Source code analysis ...

Ubuntu 18.04 installs mysql 5.7.23

I installed MySQL smoothly in Ubuntu 16.04 before...

How to make your JavaScript functions more elegant

Table of contents Object parameters using destruc...

A brief discussion on why daemon off is used when running nginx in docker

I'm very happy. When encountering this proble...

Non-standard implementation code for MySQL UPDATE statement

Today I will introduce to you a difference betwee...

Optimizing query speed of MySQL with tens of millions of data using indexes

1. The role of index Generally speaking, an index...

Steps to introduce PWA into Vue project

Table of contents 1. Install dependencies 2. Conf...

WeChat applet implements SMS login in action

Table of contents 1. Interface effect preview 2.u...