Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get one day ahead of a given date?

Tags:

sql

Suppose I have a date 2010-07-29. Now I would like to check the result of one day ahead. how to do that

For example,

SELECT * 
  from table 
 where date = date("2010-07-29")

How to do one day before without changing the string "2010-07-29"?

I searched and get some suggestion from web and I tried

SELECT * 
  from table 
 where date = (date("2010-07-29") - 1 Day)

but failed.

like image 691
skydoor Avatar asked Jul 29 '10 20:07

skydoor


People also ask

How do you add one day to a new Date?

var date = new Date(); // add 1 day date. setDate(date. getDate() + 1);

Why a given Date turns into a day before when using new Date?

The format you're using is interpreted as being a UTC date, so the time is assumed to be midnight in Western Europe. That's 8 hours ahead of you. You can force the time zone to be interpreted by tacking on T00:00-0800 to the date string.

How do I get the day before JavaScript?

Use the setDate() method to get the previous day of a date, e.g. date. setDate(date. getDate() - 1) . The setDate method takes the day of the month as a parameter and changes the value for the Date instance.

How can I increment a Date by one day in JavaScript?

To increment a JavaScript date object by one or more days, you can use the combination of setDate() and getDate() methods that are available for any JavaScript Date object instance. The setDate() method allows you to change the date of the date object by passing an integer representing the day of the month.


3 Answers

MySQL

SELECT * 
  FROM TABLE t
 WHERE t.date BETWEEN DATE_SUB('2010-07-29', INTERVAL 1 DAY)
                  AND '2010-07-29'

Change DATE_SUB to DATE_ADD if you want to add a day (and reverse the BETWEEN parameters).

SQL Server

SELECT *
  FROM TABLE t
 WHERE t.date BETWEEN DATEADD(dd, -1, '2010-07-29')
                  AND '2010-07-29'

Oracle

SELECT *
  FROM TABLE t
 WHERE t.date BETWEEN TO_DATE('2010-07-29', 'YYYY-MM-DD') - 1
                  AND TO_DATE('2010-07-29', 'YYYY-MM-DD')

I used BETWEEN because the date column is likely DATETIME (on MySQL & SQL Server, vs DATE on Oracle), which includes the time portion so equals means the value has to equal exactly. These queries give you the span of a day.

like image 103
OMG Ponies Avatar answered Sep 19 '22 15:09

OMG Ponies


If you're using Oracle, you can use the + and - operators to add a number of days to a date.

http://psoug.org/reference/date_func.html

Example:

SELECT SYSDATE  +  1 FROM dual;

Will yield tomorrow's date.

If you're not using Oracle, please tell use what you ARE using so we can give better answers. This sort of thing depends on the database you are using. It will NOT be the same across different databases.

like image 2
FrustratedWithFormsDesigner Avatar answered Sep 20 '22 15:09

FrustratedWithFormsDesigner


Depends of the DateTime Functions available on the RDBMS

For Mysql you can try:

mysql> SELECT DATE_ADD('1997-12-31',
->                 INTERVAL 1 DAY);


mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
    -> '1997-12-02'
like image 1
Andreas Avatar answered Sep 18 '22 15:09

Andreas