REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
How to get today’s date in MySQL
Preamble
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.
How to get Today’s Date in SQL/ Teradata / MySQL & Get Yesterday’s Date
Enteros
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of RDBMS, NoSQL, and machine learning database platforms.
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...