Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - NOW() + 10 days

I can't find solution, I'm trying to take in my database date of event that are 10 days after now.

I tried :

SELECT * FROM XXX.Vente WHERE date > (now()+40);

and :

SELECT * FROM LeVigneau.Vente WHERE date > now()+INTERVAL 10 DAY;

But it doesn't work. Du you have an idea ? Thanks a lot

like image 280
Romain Caron Avatar asked Dec 22 '15 14:12

Romain Caron


People also ask

What does NOW () do in SQL?

The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

How can I add 15 days to current date in MySQL?

Use the DATE_ADD() function if you want to increase a given date in a MySQL database. In our example, we increased each start date by two days.

How do I subtract 3 days from a date in SQL?

The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.


2 Answers

You have to use backticks on date, that because DATE is reserved keyword and DATE_ADD function in following:

Syntax

DATE_ADD(date,INTERVAL expr type)

Query

SELECT * FROM LeVigneau.Vente WHERE `date` > DATE_ADD(now(), INTERVAL 10 DAY);

Also use >= or =, It depends on what exactly do you need, to get records only for 10th day from now or from 10 days and later.

like image 141
Stanislovas Kalašnikovas Avatar answered Oct 08 '22 02:10

Stanislovas Kalašnikovas


For exactly 10 days:

SELECT * FROM LeVigneau.Vente WHERE `date` = DATE_ADD(now(), INTERVAL 10 DAY);

All other solution give more then 10 days, not exactly 10 days.

for 10 days or more:

SELECT * FROM LeVigneau.Vente WHERE `date` >= DATE_ADD(now(), INTERVAL 10 DAY);
like image 32
davejal Avatar answered Oct 08 '22 04:10

davejal