MySQL Date Compare Medium
MySQL Date Compare Medium
com/mysql-date-compare-973691343c70
Search Medium
--
1 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70
2 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70
Following is a simple example that explains the difference between both data
types.
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:
4 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70
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.
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
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)
);
7 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70
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.
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
Query
use sakila;
select customer_id "CustomerID",first_name "First Name",last_name "Last Name",email "Email ID", re
Output
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.
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.
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
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
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
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
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
-- --
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
25 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70
-- -- 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.
26 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70
-- -- 107
27 of 28 26/06/2023, 12:08
MySQL Date Compare | Medium https://jordansanders.medium.com/mysql-date-compare-973691343c70
-- 11 -- 453
Help Status Writers Blog Careers Privacy Terms About Text to speech Teams
28 of 28 26/06/2023, 12:08