SQL BETWEEN 2 date values HELP
SELECT * FROM myDateTable
WHERE date_Start
OR date_end
BETWEEN DateValue('" & CoverMonth_start & "')
AND DateValue('" & CoverMonth_end & "')"
The CoverMonth_start
a开发者_Python百科nd _end
dates are looping from January to December. This query is supposed to select only the records WHERE date_Start OR date_end BETWEEN DateValue...
. But this query is selecting all the records in the database.
Your query isn't doing what you think it is.
The OR
is your problem - the moment the date_start is populated, that record will be returned.
This is probably the query that you want:
SELECT * FROM myDateTable
WHERE (date_Start
BETWEEN DateValue('" & CoverMonth_start & "')
AND DateValue('" & CoverMonth_end & "')")
OR (date_end
BETWEEN DateValue('" & CoverMonth_start & "')
AND DateValue('" & CoverMonth_end & "')")
This is because you are doing date_Start OR....
, this is selecting all the records that has ANY value in date_Start
and date_End
is in the given values range.
What you should write is this:
SELECT * FROM myDateTable WHERE
date_Start BETWEEN DateValue('" & CoverMonth_start & "') AND DateValue('" & CoverMonth_end & "')"
OR
date_end BETWEEN DateValue('" & CoverMonth_start & "') AND DateValue('" & CoverMonth_end & "')"
SELECT * FROM myDateTable WHERE date_Start between date1 and date2 OR date_end BETWEEN date1 and date2
SELECT *
FROM myDateTable
WHERE (date_start BETWEEN .... AND ....)
OR
(date_end BETWEEN .... AND ....)
OR
has lower precedence than BETWEEN
.
WHERE date_Start OR date_end BETWEEN a AND b
translates to
WHERE CAST(date_Start AS BOOLEAN) = true OR ( date_end BETWEEN a AND b)
what you want is... oh see other answers (an easy question, isnt'it?)
精彩评论