SQL 在两列之间选择日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7190568/
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 11:52:26  来源:igfitidea点击:

Select Date Between Two Columns

sql

提问by ConquistadorAravinth

I need a query in SQL.
If I have two columns STARTDATEand END_DATE.
I want to select a all rows where a date falls between these two dates.

我需要一个 SQL 查询。
如果我有两列STARTDATEEND_DATE.
我想选择日期落在这两个日期之间的所有行。

e.g.: startdate = 1/1/2011 AND enddate = 2/2/2011.

例如:startdate = 1/1/2011 AND enddate = 2/2/2011。

回答by Johan

SELECT * FROM table1 
WHERE '2011-01-01' BETWEEN table1.startdate AND table1.enddate

Replace the explicit date either with now()or a parameter or whatever.

now()或 参数或其他任何东西替换显式日期。

If the enddate is notdefined as NOT NULLyou can do:

如果结束日期未按NOT NULL您的方式定义:

SELECT * FROM table1 
WHERE '2011-01-01' BETWEEN table1.startdate AND COALESCE(table1.enddate, NOW())

See: http://www.1keydata.com/sql/sql-coalesce.html

参见:http: //www.1keydata.com/sql/sql-coalesce.html

回答by Jean-Charles

Does this help you ?

这对你有帮助吗?

select * 
from table 
where START_DATE < NOW() AND END_DATE > NOW()

Depending on the database, use CURRENT_TIMESTAMP() or TODAY()

根据数据库,使用 CURRENT_TIMESTAMP() 或 TODAY()

回答by John N

Do you mean this:

你的意思是:

select *
from mytable
where start_date >= '01/01/2011'
and end_date <= '02/01/2011'

Until you do more to clarify your question, it's hard for us to provide better answers.

在您进一步澄清问题之前,我们很难提供更好的答案。