SQl query help- Between 2 date ranges

Hello,Iam running the below query but the problem is its not returning any rows for '2013-08-27'  inclusive.Its returning only until 2013-08-26.Please advice.



  SELECT COUNT(patientslog.patid),CONVERT(char(10),changedon,101)
 FROM   ("FwReports"."dbo"."PatientsLog" "PatientsLog" LEFT OUTER JOIN "FwReports"."dbo"."Patients" "Patients" ON 
 ("PatientsLog"."FacID"="Patients"."FacID") AND ("PatientsLog"."PatID"="Patients"."PatID")) 
 LEFT OUTER JOIN "FwReports"."dbo"."Facilities" "Facilities" ON "PatientsLog"."FacID"="Facilities"."FacID"
 WHERE   NOT ("Patients"."PatLName"='-' OR "Patients"."PatLName"='.')and PatientsLog.Comment like '%Patient added.%' 
  AND PatientsLog.ChangedOn >= '2013-06-29' and PatientsLog.ChangedOn < ='2013-08-27' 
  
  group by CONVERT(char(10),changedon,101)
 ORDER BY  CONVERT(char(10),changedon,101)DESC

Open in new window

Star79Asked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>WHERE   NOT ("Patients"."PatLName"='-' OR "Patients"."PatLName"='.')and PatientsLog.Comment like '%Patient added.%'
  AND PatientsLog.ChangedOn >= '2013-06-29' and PatientsLog.ChangedOn < ='2013-08-27'

That's likely because the last part of the above WHERE clause is filtering values in the ChangedON colum to be between (incluive) June 29 and August 27, which is going to be interpreted as '2013-08-27 00:00:00.000' (aka Midnight), so that any activity after midnight on that day will not be returned.

You'll probably want to change the T-SQL to one of these two..
WHERE  ... PatientsLog.ChangedOn < ='2013-08-27 23:59:59.000'
or
WHERE  ... PatientsLog.ChangedOn < '2013-08-28'
or <but takes longer to execute>
WHERE  CAST(PatientsLog.ChangedOn as date) BETWEEN  '2013-06-29' and '2013-08-27'
0

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
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
btw SQL Expert PortletPaul wrote an excellent article on the dangers of between in dates.
0
Star79Author Commented:
so can it be changed to > and < sign for dates
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Yes, but that's not the root what you are experiencing.

Which is '2013-08-27' is the same as '2013-08-27 00:00:00', which is less than
   8am at '2013-08-27 08:00:00',
   or noon at '2013-08-27 12:00:00',
   or 5pm at '2013-08-27 17:00:00'
0
Star79Author Commented:
so please let me know what is the soltuion for one case like this
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
I made an assumption that you were able to take my first comment and apply it to the T-SQL in your question.  If that was not correct, here you go...

 
SELECT COUNT(patientslog.patid),CONVERT(char(10),changedon,101)
 FROM   ("FwReports"."dbo"."PatientsLog" "PatientsLog" LEFT OUTER JOIN "FwReports"."dbo"."Patients" "Patients" ON 
 ("PatientsLog"."FacID"="Patients"."FacID") AND ("PatientsLog"."PatID"="Patients"."PatID")) 
 LEFT OUTER JOIN "FwReports"."dbo"."Facilities" "Facilities" ON "PatientsLog"."FacID"="Facilities"."FacID"
 WHERE   NOT ("Patients"."PatLName"='-' OR "Patients"."PatLName"='.')and PatientsLog.Comment like '%Patient added.%' 
  AND PatientsLog.ChangedOn >= '2013-06-29' and PatientsLog.ChangedOn < ='2013-08-27 23:59:59.000' 
  
  group by CONVERT(char(10),changedon,101)
 ORDER BY  CONVERT(char(10),changedon,101)DESC

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
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 2008

From novice to tech pro — start learning today.