I have a start_date
and end_date
. I want to get the list of dates in between these two dates. Can anyone help me pointing the mistake in my query.
select Date,TotalAllowance
from Calculation
where EmployeeId=1
and Date between 2011/02/25 and 2011/02/27
Here Date
is a datetime
variable.
Check below Examples: Both working and Non-Working.
OR
OR
AND below is not working:
I would go for
The logic being that
>=
includes the whole start date and<
excludes the end date, so we add one unit to the end date. This can adapted for months, for instance:Since a datetime without a specified time segment will have a value of
date 00:00:00.000
, if you want to be sure you get all the dates in your range, you must either supply the time for your ending date or increase your ending date and use<
.OR
OR
DO NOT use the following, as it could return some records from 2011/02/28 if their times are 00:00:00.000.
Try this:
The date values need to be typed as strings.
To ensure future-proofing your query for SQL Server 2008 and higher,
Date
should be escaped because it's a reserved word in later versions.Bear in mind that the dates without times take midnight as their defaults, so you may not have the correct value there.