This is because you're misunderstanding how dates work. This is further evidenced by the fact that you're passing and using your dates as strings
First off, dates internally to sql server are expressed as numbers of days since a certain date (midnight on 1/1/1900)
This means a date of 2 jan 1900 is internally expressed by sqlserver as 1.0. A date of 1800 hours on 2 jan 1900 is expressed internally as 1.75 - because there have been 1.75 days since midnight on the first. 6pm is three quarters the way through a 24 hour day hence the .75 part in the internal representation
With me so far?
it is the decimal point part that is defeating your logic
Let's see an example
Lets say you want records between 1 jan 1900 and 10 jan 1900, so really, you want records that have a time anything up to 23:59:59 (and 999999... milliseconds) on 10 jan 1900
This means you want records that are between 0.0 and 9.999999999 days after midnight on 1 jan....
But when you're running your query, you're just asking for:
BETWEEN #1 jan 1900# and #10 jan 1900#
In SQL terms this is
BETWEEN 0.0 and 9.0
And not what you want:
BETWEEN 0.0 and 9.9999999999999999999999999....
Ultimately, midnight is the very first thing that happens on a day. You won't get any records for 6am on the 10th jan because that is WELL AFTER midnight, it is a decimal number like 9.25
I'm sure you can appreciate that 9.25 is NOT BETWEEN 0.0 and 9.0
You will however get records that occurred exactly bang on midnight on the 10th, because they would internally be represented as 9.0, and 9.0 is between 0.0 and 9.0
So you need to alter the way you are doing your query:
date >= startdate AND date < enddate_plus_one_day
i.e. in internal date representation tersm if you want yo get the dates that are 6am, i.e. the 9.25 days after 1 jan 1900, then you need to be querying date >= 0.0 and date < 10.0
- this will return the records all the way up to and including 23:59:59
I also complained at your querying style - youre passing dates as strings and hoping that sqlserver will figure you out. Don't do this. Be explicit:
SELECT *
FROM ProjectDetails
where SubmittedDate >= CONVERT(datetime, @Year, 112)--pass date as string in yyyymmdd
AND SubmittedDate < (CONVERT(datetime, @Rtype, 112)+1.0) --pass date as a string in yyyymmdd