Compare only MySQL timestamp dates to date parameters
In this article we will use the DATE()
, CAST()
, and CONVERT()
functions to compare MySQL timestamp dates with only the date parameter.
DATE() vs. CAST() vs. CONVERT() in MySQL
Below is a brief description of each function. You can also find more examples of each by clicking here.
DATE() Method in MySQL
DATE()
The method extracts the date portion from a timestamp or DateTime expression. It has only one parameter, which must be of type DATE, TIMESTAMP, or DATETIME.
If the passed argument is not of the above type, NULL is returned. This feature is available if you are using MySQL 4.0 or later.
CONVERT() Method in MySQL
CONVERT()
Method converts a specific value to a specified data type. For example, we can convert a value of type TIMESTAMP to type DATE.
It has two parameters, both of which are required. The first parameter is the value to be converted, and the other is the data type to which the value should be converted.
It is available in MySQL 4.0 and above.
CAST() Method in MySQL
CAST()
method is similar to what was explained above CONVERT()
. We use it to convert a value of one data type to another data type.
For example, when we convert the value of DateTime to DATE type. Like CONVERT()
the method, it also has two values: the value to be converted and the data type to be converted.
If we have MySQL 4.0 or above, we can use this feature.
Compare only MySQL timestamp dates to date parameters
To compare only MySQL timestamp dates with the date parameter, we have created a table called date_comparison with two attributes ID and COL_DATETIME. Here ID is of type int and COL_DATETIME is TIMESTAMP.
You can also create this table using the following query to follow along with this tutorial.
example:
# Create a table
CREATE TABLE date_comparison(
ID INT NOT NULL AUTO_INCREMENT,
COL_DATETIME TIMESTAMP NOT NULL,
PRIMARY KEY (ID));
# Insert data
INSERT INTO date_comparison(col_datetime)
VALUES
('2001-11-15 09:50:00'),
('2006-08-09 04:30:00'),
('2001-11-15 23:30:00'),
('2005-06-03 06:22:11'),
('2004-01-01 21:42:17');
# Show all data
SELECT * FROM date_comparison;
Output:
+----+---------------------+
| ID | col_datetime |
+----+---------------------+
| 1 | 2001-11-15 09:50:00 |
| 2 | 2006-08-09 04:30:00 |
| 3 | 2001-11-15 23:30:00 |
| 4 | 2005-06-03 06:22:11 |
| 5 | 2004-01-01 21:42:17 |
+----+---------------------+
5 rows in set (0.00 sec)
Now, we want to retrieve records for a specific date; for example, only for the date 2005-06-03. We need to compare the date parameter with all col_datetime column values of the TIMESTAMP type.
Use DATE() to compare only MySQL timestamp dates to date parameters
So we can only compare the timestamp date with the date parameter as follows.
example:
SELECT * FROM date_comparison WHERE DATE(col_datetime) = '2005-06-03';
Output:
+----+---------------------+
| ID | col_datetime |
+----+---------------------+
| 4 | 2005-06-03 06:22:11 |
+----+---------------------+
1 row in set (0.00 sec)
If we are only interested in the date, we can DATE()
convert col_datetime to a date using the function.
example:
SELECT ID, DATE(col_datetime) FROM date_comparison
WHERE DATE(col_datetime) = '2005-06-03';
Output:
+----+--------------------+
| ID | DATE(col_datetime) |
+----+--------------------+
| 4 | 2005-06-03 |
+----+--------------------+
1 row in set (0.00 sec)
Use CAST() to compare only MySQL timestamp dates to date parameters
We can also use the CAST() function to compare a timestamp date with a date parameter.
example:
SELECT ID, CAST(col_datetime AS DATE) FROM date_comparison
WHERE CAST(col_datetime AS DATE) = '2005-06-03';
Output:
+----+----------------------------+
| ID | CAST(col_datetime AS DATE) |
+----+----------------------------+
| 4 | 2005-06-03 |
+----+----------------------------+
1 row in set (0.00 sec)
Use CONVERT() to compare only MySQL timestamp dates to date parameters
CONVERT()
Functions can also compare just the timestamp to the date.
example:
SELECT ID, CONVERT(col_datetime, DATE) FROM date_comparison
WHERE CONVERT(col_datetime, DATE) = '2005-06-03';
Output:
+----+-----------------------------+
| ID | CONVERT(col_datetime, DATE) |
+----+-----------------------------+
| 4 | 2005-06-03 |
+----+-----------------------------+
1 row in set (0.00 sec)
Use BETWEEN to compare only MySQL timestamp dates to date parameters
If you have an index on the col_datetime column (which has a TIMESTAMP type), the following solution will be the fastest because it can use the index on the col_datetime column.
example:
SELECT * FROM date_comparison
WHERE col_datetime
BETWEEN '2005-06-03 00:00:00' AND '2005-06-03 23:59:59';
Output:
+----+---------------------+
| ID | col_datetime |
+----+---------------------+
| 4 | 2005-06-03 06:22:11 |
+----+---------------------+
1 row in set (0.00 sec)
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:192 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:195 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Installing MySQL Client in Linux
Publish Date:2025/04/22 Views:186 Category:MySQL
-
MySQL is an abbreviation for Structured Query Language which is a relational database management system. It is an open source database which is freely available and used for both large and small applications. The use cases are in school app
Subtraction in MySQL
Publish Date:2025/04/21 Views:140 Category:MySQL
-
MINUS Operator is used in SQL to find unique elements in table A that are not present in table B. Through this tutorial, we will see how to emulate the operator in MySQL MINUS to get the desired result. We will understand it by using NOT IN
INTERSECT Operator in MySQL
Publish Date:2025/04/21 Views:99 Category:MySQL
-
This article will help you understand INTERSECT the operator. Although MySQL does not support INTERSECT and MINUS / EXCEPT , there are other ways to simulate this functionality. We will see INTERSECT what is , its benefits, and learn variou
Deleting using Join in MySQL
Publish Date:2025/04/21 Views:158 Category:MySQL
-
This tutorial article will show you how to use MySQL JOIN methods to delete data from multiple tables. This is useful when you are simultaneously deleting records from one table that are related to a specific record in another table. DELETE
MySQL using LIKE in case insensitive search
Publish Date:2025/04/21 Views:131 Category:MySQL
-
This tutorial article will show you how to use LIKE the operator to search a column. We will show you how to use it correctly to include all results that meet certain criteria, regardless of whether the value is uppercase or lowercase. LIKE
Loop PHP MySQLi Get Array Function
Publish Date:2025/04/21 Views:105 Category:MySQL
-
MySQLi fetch function is used to access data from the database server. After fetching the data, you can also iterate over it MySQLi with queries. In this article, we will see mysqli_fetch_array() the use of functions and methods to iterate
Generate a random and unique string in MySQL
Publish Date:2025/04/21 Views:78 Category:MySQL
-
Today, we will learn to use various functions in MySQL to generate random and unique strings. These functions include MD5() , RAND() , , SUBSTR() and UUID() . There is no inbuilt method to generate random string in MySQL, but there are many