SQL 选择两列中两个日期之间的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1479080/
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
Select records between two dates in two columns
提问by Quintin Robinson
How do I Select records between two dates in two columns?
如何在两列中的两个日期之间选择记录?
Select * From MyTable Where 2009-09-25 is between ColumnDateFrom to ColumnDateTo
I have a date (2009-09-25) and I like to select the records that is in the timeframe ColumnDateFrom to ColumnDateTo.
我有一个日期 (2009-09-25),我喜欢选择 ColumnDateFrom 到 ColumnDateTo 时间范围内的记录。
Sample
样本
Record 1 ColumnDateFrom = 2009-08-01 AND ColumnDateTo = 2009-10-01
Record 2 ColumnDateFrom = 2010-08-01 AND ColumnDateTo = 2010-10-01
记录 1 ColumnDateFrom = 2009-08-01 AND ColumnDateTo = 2009-10-01
记录 2 ColumnDateFrom = 2010-08-01 AND ColumnDateTo = 2010-10-01
If my input date is 2009-09-28; then I get record 1
如果我的输入日期是 2009-09-28;然后我得到记录 1
回答by Quintin Robinson
Standard Between should work (T-SQL).
标准之间应该工作(T-SQL)。
SELECT * FROM MyTable WHERE @MYDATE BETWEEN ColumnDateFrom AND ColumnDateFrom
回答by KM.
if I understand correctly, try this:
如果我理解正确,请尝试以下操作:
SELECT
*
FROM MyTable
WHERE ColumnDateFrom <= '2009-09-25' AND ColumnDateTo >= '2009-09-25'
回答by Yannick Motton
Try this:
尝试这个:
SELECT * FROM MyTable WHERE '2009-09-25' BETWEEN ColumnDateFrom AND ColumnDateTo
回答by e11s
select *
from MyTable
where ColumnDateFrom <= '2009-09-25'
and ColumnDateTo >= '2009-09-25'
回答by Anatoliy
mysql:
mysql:
select * from MyTable where '2009-09-25' between ColumnDateFrom and ColumnDateTo
回答by Charles Bretana
Just remove the "Is"
只需删除“是”
Select * From MyTable
Where '2009-09-25' Between ColumnDateFrom to ColumnDateTo
remember to consider the time portiomn if the column values have date and time in them... (assuming DateOnly() is some function that strips the time from a datetime column)
如果列值中包含日期和时间,请记住考虑时间部分......(假设 DateOnly() 是一些从日期时间列中剥离时间的函数)
Select * From MyTable
Where '2009-09-25' Between DateOnly(ColumnDateFrom)
To DateOnly(ColumnDateTo)
回答by mdli
here example for ms access vba
这里是ms access vba的例子
"[LearningBegin]<= " & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and [LearningEnd]>=" + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#") + _
" or [LearningBegin]>= " & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and [LearningEnd]<=" + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#") + _
" or [LearningBegin] between " & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and " + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#") + " and [LearningEnd]<=" + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#") + _
" or [LearningBegin] between " & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and " + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#") + " and [LearningEnd]>=" + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#") + _
" or [LearningBegin] >= " & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and [LearningEnd] between" & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and " + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#") + _
" or [LearningBegin] <= " & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and [LearningEnd] between" & Format(eFilterBegin.Value, "\#mm\/dd\/yyyy\#") + " and " + Format(eFilterEnd.Value, "\#mm\/dd\/yyyy\#")