选择两列中两个日期之间的记录

时间:2009-09-25 19:14:58

标签: sql sql-server sql-server-2005 tsql

如何在两列中的两个日期之间选择记录?

Select * From MyTable Where 2009-09-25 is between ColumnDateFrom to ColumnDateTo

我有一个日期(2009-09-25),我喜欢选择时间范围ColumnDateFrom到ColumnDateTo的记录。

示例

  

记录1 ColumnDateFrom = 2009-08-01 AND ColumnDateTo = 2009-10-01

     

记录2 ColumnDateFrom = 2010-08-01 AND ColumnDateTo = 2010-10-01

如果我的输入日期是2009-09-28;然后我得到记录1

7 个答案:

答案 0 :(得分:6)

标准之间应该工作(T-SQL)。

SELECT * FROM MyTable WHERE @MYDATE BETWEEN ColumnDateFrom AND ColumnDateFrom

答案 1 :(得分:4)

如果我理解正确,试试这个:

SELECT
    *
    FROM MyTable 
    WHERE ColumnDateFrom <= '2009-09-25' AND ColumnDateTo >= '2009-09-25'

答案 2 :(得分:4)

试试这个:

SELECT * FROM MyTable WHERE '2009-09-25' BETWEEN ColumnDateFrom AND ColumnDateTo

答案 3 :(得分:1)

select * 
from MyTable 
where ColumnDateFrom <= '2009-09-25' 
    and ColumnDateTo >= '2009-09-25'

答案 4 :(得分:0)

MySQL的:

select * from MyTable where '2009-09-25' between ColumnDateFrom and ColumnDateTo

答案 5 :(得分:0)

只需删除“是”

即可
Select * From MyTable 
Where '2009-09-25' Between ColumnDateFrom to ColumnDateTo

如果列值中包含日期和时间,请记得考虑时间portiomn ... (假设DateOnly()是一个从日期时间列中删除时间的函数)

Select * From MyTable 
Where '2009-09-25' Between DateOnly(ColumnDateFrom) 
                        To DateOnly(ColumnDateTo)

答案 6 :(得分:0)

此处为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\#")