查找开始日期和结束日期之间的日期

时间:2010-04-15 10:14:03

标签: sql mysql

我想检查日期是否在开始日期和结束日期之间。

我添加了一个where子句

where datepaid between '2010-04-01' AND '2010-04-15'

但问题在于它排除了“2010-04-15”。

它应该包括结束日期,我该怎么做?

请帮我解决这个问题

此致

的Pankaj

7 个答案:

答案 0 :(得分:14)

明确指定时间部分:

WHERE datepaid BETWEEN '2010-04-01 00:00:00' AND '2010-04-15 23:59:59'

答案 1 :(得分:5)

也许您的日期包括日内时间。试试这个:

where '2010-04-01' <= datepaid AND datepaid < '2010-04-16'

答案 2 :(得分:1)

WHERE   datepaid >= '2010-04-01'
        AND datepaid < '2010-04-15' + INTERVAL 1 DAY

答案 3 :(得分:0)

where datepaid between '2010-04-01' AND '2010-04-15 23:59:59'

答案 4 :(得分:0)

这可能是由于约会有时间:

select
    '2010-04-15' between '2010-04-01' AND '2010-04-15', -- 1
    '2010-04-15 12:00:00' between '2010-04-01' AND '2010-04-15', -- 0
    '2010-04-15 12:00:00' between '2010-04-01 00:00:00' AND '2010-04-15 23:59:59' -- 1

在比较之前将时间添加到范围分隔符或截断值:

select
    '2010-04-15 12:00:00' between '2010-04-01 00:00:00' AND '2010-04-15 23:59:59' -- 1
    cast('2010-04-15 12:00:00' as date) between '2010-04-01' AND '2010-04-15' -- 1

我没有检查,但我认为没有施法会有更好的表现。

答案 5 :(得分:0)

“2010-04-01”和“2010-04-15”之间的CONVERT(VARCHAR(10),datepaid,23)

答案 6 :(得分:-1)

添加此子句:

OR datepaid = '2010-04-15'