Keshav Kwatra
Keshav Kwatra

Reputation: 11

mysql query for finding date of 4days after current date

what is the query to find out date which should be after 4 days of curdate I tried

select curdate()+4

But it is giving the wrong answer.

Please help me out.

Upvotes: 1

Views: 382

Answers (3)

davejal
davejal

Reputation: 6143

Kostas Mitsarakis is a good solution, but you also have other options:

1.using date_add function

    SELECT DATE_ADD( CURDATE( ) , INTERVAL 4 DAY );

2. using adddate function

    SELECT ADDDATE(curdate(), INTERVAL 4 DAY);

Upvotes: 0

JRD
JRD

Reputation: 1987

curdate() returns either a string or an integer depending on the context it is used.

SELECT cast(curdate() as char) date_s,
       cast(curdate() as unsigned integer) date_i,
       curdate() + 4 bad,
       curdate() + interval 4 day as "interval";

+------------+----------+----------+------------+
| date_s     | date_i   | bad      | interval   |
+------------+----------+----------+------------+
| 2015-10-28 | 20151028 | 20151032 | 2015-11-01 |
+------------+----------+----------+------------+

In your case, curdate() is returning 20151028 in number context, then adding 4 to it, to get 20151032. Using interval N day with +/- is somewhat of a special case, as it implicitly converts the other operand to a date and does the correct date math.

Upvotes: 0

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

You can use this:

SELECT CURDATE() + INTERVAL 4 DAY;

Upvotes: 2

Related Questions