Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

MySQL Date Compare | Medium https://jordansanders.medium.

com/mysql-date-compare-973691343c70

Search Medium

MySQL Date Compare


Jordan Sanders · Follow
8 min read · Mar 27

--

1 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

This article will give us a walk-through of different methods to compare


datetime values in MySQL. In MySQL, we can store dates in DATE and
TIMESTAMP data types. Both data types store data in YYYY-MM-DD
HH:MM: SS format. There are a few differences between both datatypes.

1. The DATETIME supports ‘1000–01–01 00:00:00’ to


‘9999–12–31 23:59:59’ but TIMESTAMP supports ‘1970–01–01

2 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

00:00:01’ to ‘2038–01–19 03:14:07 UTC.

2. The DATETIME is constant, but the TIMESTAMP values change


based on the timezone the application uses because the TIMESTAMP
data will convert the current time to UTC and vice-versa.

Following is a simple example that explains the difference between both data
types.

Query 1: DATE TIME datatype

Query 2: TIMESTAMP datatype

3 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Now, change the timezone from IST (Indian Standard Time) to CST (Central
Standard Time). After changing timezones, the query output changes like the
following:

Query 1: DATE TIME datatype

4 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Query 2: TIMESTAMP datatype

5 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

The above screenshot shows that the timestamp is converted to the CST
(UTC -6:00) timezone.

This article contains various queries, so I have prepared a demo setup on my


computer to understand it more clearly. The details are following:

Demo Setup
For demonstration, I have created a table named tblCustomer in the sakila

6 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

database. The SQL Code to create the table is below.

USE sakila;
CREATE TABLE tblCustomer (
customer_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
rental_id smallint UNSIGNED NOT NULL,
rental_date datetime,
return_date datetime,
PRIMARY KEY (customer_id)
);

I have inserted dummy records in a tblCustomer table. The SQL Code to


insert data is the following:

INSERT INTO sakila.tblcustomer(customer_id, first_name, last_name, email, rental_id, rental_date,


(1, 'PATRICIA', 'JOHNSON', 'PATRICIA.JOHNSON@sakilacustomer.org', 320, '2005–05–27 00:09:24', '200
(2, 'TYLER', 'WREN', 'TYLER.WREN@sakilacustomer.org', 322, '2005–05–27 00:47:35', '2005–06–02 00:3
(3, 'CLARA', 'SHAW', 'CLARA.SHAW@sakilacustomer.org', 323, '2005–05–27 00:49:27', '2005–05–30 03:1
(4, 'DAVE', 'GARDINER', 'DAVE.GARDINER@sakilacustomer.org', 487, '2005–05–28 00:00:30', '2005–05–2
(5, 'REGINA', 'BERRY', 'REGINA.BERRY@sakilacustomer.org', 488, '2005–05–28 00:07:50', '2005–06–03
(6, 'DERRICK', 'BOURQUE', 'DERRICK.BOURQUE@sakilacustomer.org', 489, '2005–05–28 00:09:12', '2005–
(7, 'SUE', 'PETERS', 'SUE.PETERS@sakilacustomer.org', 683, '2005–05–29 00:09:48', '2005–06–02 04:2

7 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

(8, 'DEREK', 'BLAKELY', 'DEREK.BLAKELY@sakilacustomer.org', 684, '2005–05–29 00:13:15', '2005–06–0


(9, 'BERNICE', 'WILLIS', 'BERNICE.WILLIS@sakilacustomer.org', 685, '2005–05–29 00:17:51', '2005–06
(10, 'NORMAN', 'CURRIER', 'NORMAN.CURRIER@sakilacustomer.org', 686, '2005–05–29 00:27:10', '2005–0
(11, 'JO', 'FOWLER', 'JO.FOWLER@sakilacustomer.org', 687, '2005–05–29 00:32:09', '2005–05–31 23:53
(14, 'BERNICE', 'WILLIS', 'BERNICE.WILLIS@sakilacustomer.org', 837, '2005–05–30 00:02:08', '2005–0
(15, 'NATHANIEL', 'ADAM', 'NATHANIEL.ADAM@sakilacustomer.org', 838, '2005–05–30 00:27:57', '2005–0
(17, 'DANIELLE', 'DANIELS', 'DANIELLE.DANIELS@sakilacustomer.org', 995, '2005–05–31 00:06:02', '20
(18, 'ERIC', 'ROBERT', '@sakilacustomer.org">ERIC.ROBERT@sakilacustomer.org', 996, '2005–05–31 00:
(19, 'NATALIE', 'MEYER', 'NATALIE.MEYER@sakilacustomer.org', 997, '2005–05–31 00:08:25', '2005–06–
(20, 'ALAN', 'KAHN', 'ALAN.KAHN@sakilacustomer.org', 998, '2005–05–31 00:16:57', '2005–06–01 22:41
(21, 'MARVIN', 'YEE', 'MARVIN.YEE@sakilacustomer.org', 999, '2005–05–31 00:25:10', '2005–06–03 06:

An overview of the DATE() function.


The DATE() function shows the only date part from the datetime expression.
If the expression is NULL, then it also returns NULL. Following is the
syntax of the DATE() function.

Select DATE(datetime expression)

Let us take a simple example. We want to display the date part of


rental_date from the tblCustomer table. To achieve that, we can write the
query as follows:

8 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Output

As you can see, the query extracted the date part of the rental_date
column.

The DATE() function compares a string expression with a datetime value.


For example, we want to extract the list of customers whose rental date is
28–05–2005. To do that, the query is written as follows:

9 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

use sakila;
select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", re

Output:

As you can see, the query did not return any records because when we do not
include the DATE() function, the query includes the time portion of the
datetime column. So if you re-write the query and include the time portion, it

10 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

will extract the desired records.

Query

use sakila;
select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", re

Output

Now, lets us include the DATE function in a query.

11 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Query

use sakila;
select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", re

Output

As you see, the query returned a list of customers whose rental date is
2005–05–28. In this case the index is not used.

12 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

The DATE() function can be used with various clauses, operators, and
functions. Let us understand them with simple examples.

Compare two dates using the WHERE clause


Suppose we want to populate the list of employees whose rental date is
greater than “2005–05–27” and whose return date is less than
“2005–06–01”. The query is written as follows:

Query

use sakila;
select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", ,

Output

13 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

As you can see in the above screenshot, we have added the DATE() function
in the WHERE clause.

Now, let us see how we can use logical and arithmetic operators to compare
two dates.

Compare two dates using logical and arithmetic operators


For example, we want to show the list of the customers whose rental date is
greater than “2005–05–30” To do that, we will use the> (Arithmetic
Operator) Operator. The query is below:

14 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

use sakila;
select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", re

Output

Let us take another example; we want to populate the list of customers whose
return date is between “2005–05–31” and “2005–06–03”. To do that,
we are using BETWEEN (Logical Operator). The query is below:

15 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

use sakila;
select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", re

Output

Now, let us see how we can use the function to compare dates. The index is
used in this сase.

16 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Compare two dates using DATEDIFF() function


Suppose we want to count of days between rental_date and return_date.
To do that, we are using DATEDIFF() function. The query is following:

use sakila;
Select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", re

Output

17 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Compare dates using dbForge MySQL Studio


The dbForge MySQL Studio has a great feature that helps to filter data from
tables by comparing the dates. We can use this option in two ways

1. Compare the dates on all records of a table.

2. Compare the dates on the results set.

Compare the dates on all records of the table


The dbForge MySQL Studio allows viewing all records in a data viewer tab.
To do that, launch dbForge Studio for MySQL � Connect to sakila
database � Expand sakila � Expand tables � Right-click tblCustomer
� Select Retrieve Data. Following is the screenshot of database explorer.

18 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

19 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

As shown below, the data of the tblCustomer table will be displayed in a data
viewer tab.

20 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Now, we want to compare the rental date of all customers and populate the
list of the customers whose rental date exceeds 2005–05–30. To do that,
right-click on the rental_date column and select the filter option.

21 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

A dialog box filter option opens. Here you can add multiple conditions that
can be used to filter the data of a table.

We want to get the list of customers whose rental date is greater than
30–05–2005. To do that, the filter must be set as shown below

22 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Written by Jordan Sanders Follow

132 Followers
Click Apply.
PR Manager at Devart since 2010 | Obsessed with the promotion of database
development optimization

23 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

More from Jordan Sanders

As you can see in the above image, the data of tblCustomer is filtered, and you
can see the list of customers whose rental date is greater than 2005–05–31.
Jordan Sanders Jordan Sanders

Here I would
Devart Toolslike
for to show another interesting
PostgreSQL Find feature
Invalid of dbForge
Objects inStudio
Your for
Come
MySQLWith a Massive
2022. Update
It allows us Databases
to update the records directly from query output.
Devart has released an update of the DBA has a number of duties that are primarily
For example, I want to update the rental_date
dbForge product line for PostgreSQL. The…
of the customer name ALAN
targeted at supporting of database…
KHAN. The current value is 31–05–2005 00:16:57, and I want to change it
2 min read · May 25 6 min read · May 27, 2019
to 01–06–2005 00:16:57. To do that, click on the rental_date of ALAN
--
KHAN. A calendar will open as shown in the--following image:

24 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Jordan Sanders Jordan Sanders in HackerNoon.com

Delphi Security Components Some Aspects of MS SQL Server


SecureBridge Got a Huge Update Monitoring. Part 1.
Devart has recently introduced a long- Quite often, MS SQL Server users,
awaited update of SecureBridge, its robust… developers and administrators are faced wi…

2 min read · Sep 12, 2019 6 min read · Jun 6, 2019

-- --
Change the rental_date to 01–06–2005 00:16:57 and click on the refresh
data button on the top left side of the output toolbar. To verify the changes,
run a SELECT query on the tblCustomer table.
See all from Jordan Sanders

Recommended from Medium

25 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Mondoa Love Sharma in Dev Genius

SQL Queries, everything you System Design Blueprint: The


need to know to get started. Ultimate Guide
Developing a robust, scalable, and efficient
system can be daunting. However,…

· 10 min read · Mar 11 · 9 min read · Apr 20

-- -- 31

As you can see in the above image, the rental_date of the ALAN KHAN user
has been changed. This feature must be used with caution to avoid
unexpected
Lists data changes in the base table of the application.

Staff Picks Stories to Help You Level-Up


Summary 364 stories · 125 saves at Work
19 stories · 121 saves
This article explains how we can store date values in MySQL. Also, we learned
Self-Improvement 101 Productivity 101
about the DATETIME and TIMESTAMP data types and the difference
20 stories · 196 saves 20 stories · 218 saves

26 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

between them. Also, I explained various methods to compare dates in


MySQL. The dbForge Studio for MySQL makes the process of comparing
dates much easier. You can filter records directly from the output without
writing any SQL queries.

Ecky Putrady in Dev Genius JP Brown

PostgreSQL Index Best Practices What Really Happens to a Human


Foundation and best practices to set up the Body at Titanic Depths
right indexes for your PostgreSQL database. A Millisecond-by-Millisecond Explanation

· 5 min read · Mar 25 · 4 min read · 3 days ago

-- -- 107

27 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70

Hussein Nasser The PyCoach in Artificial Corner

Postgres vs MySQL You’re Using ChatGPT Wrong!


The fundamental difference between the two Here’s How to Be Ahead of 99%…
popular databases Master ChatGPT by learning prompt
engineering.

· 9 min read · Feb 6 · 7 min read · Mar 17

-- 11 -- 453

See more recommendations

Help Status Writers Blog Careers Privacy Terms About Text to speech Teams

28 of 28 26/06/2023, 12:08

You might also like