Tuesday, 30 July 2019

CURDATE() Examples – MySQL

In MySQL, the CURDATE() function is used to return the current date.
More specifically, it returns the current date as a value in ‘YYYY-MM-DD’ or YYYYMMDDformat, depending on whether the function is used in a string or numeric context.

Syntax

The syntax goes like this:
CURDATE()
So no arguments are accepted or required.
However, as mentioned, the data type of the return value will depend on the context with which it’s used. More on this below.
You can also use either of the following if you prefer:
CURRENT_DATE
CURRENT_DATE()
These are synonyms for CURDATE().

Example – String Context

Here’s an example of using CURDATE() in a string context.
SELECT CURDATE();
Result:
+------------+
| CURDATE()  |
+------------+
| 2018-06-22 |
+------------+

Example – Numeric Context

Here’s an example of using CURDATE() in a numeric context.
SELECT CURDATE() + 0;
Result:
+---------------+
| CURDATE() + 0 |
+---------------+
|      20180622 |
+---------------+
In this example I added zero to the date. But I could also have added another number.
Here’s an example where I add 3 to the current date:
SELECT CURDATE() + 3;
Result:
+---------------+
| CURDATE() + 3 |
+---------------+
|      20180625 |
+---------------+

CURRENT_DATE and CURRENT_DATE()

As mentioned, both CURRENT_DATE and CURRENT_DATE() are synonyms for CURDATE().
Here’s an example with all three together:
SELECT 
    CURRENT_DATE,
    CURRENT_DATE(),
    CURDATE();
Result:
+--------------+----------------+------------+
| CURRENT_DATE | CURRENT_DATE() | CURDATE()  |
+--------------+----------------+------------+
| 2018-06-22   | 2018-06-22     | 2018-06-22 |
+--------------+----------------+------------+

0 comments:

Post a Comment