Start date between start date and end date

I want to check if a date exists between the start date and the end date.

I 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 the end date and how can I do this?

Please help me with this.

Hi,

Pankai

+4
source share
7 answers

Indicate the time parts explicitly:

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

Perhaps your dates include intraday times. Try instead:

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

to try

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

This is probably due to dates having a 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 the range delimiters, or trim the values ​​before comparing:

 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 have not tested it, but I suppose performance is better without casting.

0
source

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

0
source

add this section:

 OR datepaid = '2010-04-15' 
-1
source

Source: https://habr.com/ru/post/1307018/


All Articles