How to get today’s date in MySQL

Akademily
2 min readSep 24, 2020
How to get today’s date in MySQL

In this article, you will learn how to query today’s date data in MySQL using the built-in date functions.

GETTING TODAY’S DATE IN MYSQL USING BUILT-IN DATE FUNCTIONS

Sometimes you may need to query the data from the table to get the rows with the date column for today, for example:

SELECT
column_list
FROM
table_name
WHERE
expired_date = today;

To get today’s date, you use the CURDATE() function as follows:

mysql> SELECT CURDATE() today;
+------------+
| today |
+------------+
| 2020-03-12 |
+------------+
1 row in set (0.00 sec)

Or you can get part of the date from the current time returned by NOW():

mysql> SELECT DATE(NOW()) today;
+------------+
| today |
+------------+
| 2020-03-12 |
+------------+
1 row in set (0.00 sec)

Thus, the request must change to:

SELECT
column_list
FROM
table_name
WHERE
expired_date = CURDATE();

If the expired_date column contains part of the date and time, you should use DATE() to extract only part of the date and compare it to the current date:

SELECT
column_list
FROM
table_name
WHERE
DATE(expired_date) = CURDATE()

CREATE YOUR STORED MYSQL FUNCTION

If you often use this CURDATE() function in your queries and want to replace it with today() to make your queries more readable, you can create your own stored function named today() as follows:

DELIMITER $$
CREATE FUNCTION today()
RETURNS DATE
BEGIN
RETURN CURDATE();
END$$
DELIMITER ;

Now you can use the today() function that you created as follows:

mysql> SELECT today();
+ — — — — +
| today() |
+ — — — — +
| 2020–03–12 |
+ — — — — +
1 row included (0.00 sec)

How about tomorrow? It should be as simple as that:

mysql> SELECT today() + interval 1 day Tomorrow;
+------------+
| Tomorrow |
+------------+
| 2017-07-09 |
+------------+
1 row in set (0.00 sec)

And yesterday’s day is also simple:

mysql> SELECT today() - interval 1 day Yesterday;
+------------+
| Yesterday |
+------------+
| 2017-07-07 |
+------------+
1 row in set (0.00 sec)

In this article, you learned how to get today’s date in MySQL using the built-in date function. You also learned how to develop your own function using a stored function in MySQL.

--

--

Akademily

We conduct reviews, guides and comparative tests of gaming laptops, monitors, graphics cards, keyboards, mouses, headsets and chairs to help you buy the best ga