SQL 如何比给定日期提前一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3366765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:01:23  来源:igfitidea点击:

How to get one day ahead of a given date?

sql

提问by skydoor

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

假设我有一个日期 2010-07-29。现在我想提前一天检查一下结果。怎么做

For example,

例如,

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

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

如何做到前一天不改变字符串“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.

但失败了。

回答by OMG Ponies

MySQL

MySQL

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

Change DATE_SUBto DATE_ADDif you want to add a day (and reverse the BETWEEN parameters).

如果要添加一天(并反转 BETWEEN 参数),请将DATE_SUB更改为DATE_ADD

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 datecolumn 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.

我使用 BETWEEN 是因为该date列可能是 DATETIME(在 MySQL 和 SQL Server 上,vs DATE 在 Oracle 上),其中包括时间部分,因此等于意味着该值必须完全相等。这些查询为您提供一天的时间。

回答by FrustratedWithFormsDesigner

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

如果您使用 Oracle,则可以使用 + 和 - 运算符为日期添加天数。

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

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

Example:

例子:

SELECT SYSDATE  +  1 FROM dual;

Will yield tomorrow's date.

将产生明天的日期。

If you're notusing 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.

如果您使用 Oracle,请告诉使用您正在使用的内容,以便我们提供更好的答案。这种事情取决于您使用的数据库。它在不同的数据库中不会相同。

回答by Andreas

Depends of the DateTime Functions available on the RDBMS

取决于 RDBMS 上可用的日期时间函数

For Mysql you can try:

对于Mysql,您可以尝试:

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


mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
    -> '1997-12-02'

回答by AllenG

If youre using MSSQL, you're looking for DateAdd() I'm a little fuzzy on the syntax, but its something like:

如果您使用的是 MSSQL,您正在寻找 DateAdd() 我对语法有点模糊,但它类似于:

Select * //not really, call out your columns
From [table]
Where date = DateAdd(dd, -1, "2010-07-29",)

Edit: This syntax should be correct: it has been updated in response to a comment.
I may have the specific parameters in the wrong order, but that should get you there.

编辑:此语法应该是正确的:它已针对评论进行了更新。
我可能有错误顺序的特定参数,但这应该会让你到达那里。

回答by user2881604

In PL SQL :select sysdate+1 from dual;

在 PL SQL 中:select sysdate+1 from dual;