tags:

views:

156

answers:

7

Hi,

I want to check whether a date is between start date and end date.

i have added a where clause

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

but the problem is that it excludes '2010-04-15'.

It should include end date also how can i do this?

Please help me on this

Regards,

pankaj

+2  A: 

Perhaps your dates include intraday times. Try this instead:

where '2010-04-01' <= datepaid AND datepaid < '2010-04-16'
Marcelo Cantos
hi thanks for replying that means i have to add 1 day interval to the end date to fetch the correct record
Pankaj Khurana
A: 

try

where datepaid between '2010-04-01' AND '2010-04-15 23:59:59'
Axarydax
i love that so many people kept their unicorn avatars!
David
A: 

add this clause:

OR datepaid = '2010-04-15'
David
This won't do anything. The `BETWEEN` clause already covers this case.
Marcelo Cantos
+6  A: 

Specify the time parts explicitly:

WHERE datepaid BETWEEN '2010-04-01 00:00:00' AND '2010-04-15 23:59:59'
LukeH
does it include like 2010-04-15 23:59:59.56?
Jens Björnhager
@Jens: As far as I'm aware, MySQL doesn't store fractional seconds. If it does support them then I'd change the clause to something like `BETWEEN '2010-04-01 00:00:00.000' AND '2010-04-15 23:59:59.999'` instead.
LukeH
+1  A: 
WHERE   datepaid >= '2010-04-01'
        AND datepaid < '2010-04-15' + INTERVAL 1 DAY
Quassnoi
A: 

It's probably due to dates having time:

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

Either add time to range delimiters or truncate values before comparison:

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

I haven't checked but I suppose performance is better without casting.

Álvaro G. Vicario
A: 

where CONVERT(VARCHAR(10),datepaid,23) between '2010-04-01' AND '2010-04-15'

Yaqub Ahmad