Date Range Check How To ?

Having a few issues trying to bring back records that ARE BETWEEN AND INCLUDE the start/end report dates

EG  ReportStartDate (DD/MM/YYY) = 20/03/2006
      ReportEndDate (DD/MM/YYY) = 26/03/2006

The Code below doesnt bring back records on the 26/03/2006 ?!

CREATE PROCEDURE spSelect_Dates
      
      @ReportStartdate      SMALLDATETIME,
      @ReportEnddate        SMALLDATETIME,
AS


SELECT       COUNT(DISTINCT userid) AS UserIdCount
FROM       USERS
WHERE       date BETWEEN @ReportStartDate AND @ReportEndDate


Cheers

JT
jturkingtonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
Try it this way:
SELECT      COUNT(DISTINCT userid) AS UserIdCount
FROM      USERS
WHERE      date BETWEEN @ReportStartDate AND @ReportEndDate + '23:59:59'
Anthony PerkinsCommented:
Actually since you are using smalldatetime and there are no seconds use this:

SELECT      COUNT(DISTINCT userid) AS UserIdCount
FROM      USERS
WHERE      date BETWEEN @ReportStartDate AND @ReportEndDate + '23:59'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jturkingtonAuthor Commented:
DATE FIELD is also SMALLDATETIME

AND

ReportStartDate/ReportEndDate will always be passed in as (YYYY-MM-DD)

Is BETWEEN the best syntax to use so i maybe use => =< ??

Cheers

JT

Aneesh RetnakaranDatabase AdministratorCommented:
jturkington,
> The Code below doesnt bring back records on the 26/03/2006 ?!
when u specify this, it looks for the values which are less than or equal '26/03/2006 00:00 am
so any values after tahat it won't show
Scott PletcherSenior DBACommented:
To avoid possible issues if datetime type is changed, from full to small or small to full, you can use this style:

WHERE      date >= @ReportStartDate AND date < DATEADD(DAY, 1, CONVERT(CHAR(8), @ReportEndDate, 112))
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.