Date range parameters are off by one day

I have a Start Date and End Date parameter in my SSRS report.  The results are off by 1 day.  For example if I enter 4/2/2015 and 4/20/2015 it will return a few results from 4/1/2015 to 4/19/2015.  I think maybe it's a problem with my date conversion in T-SQL, but thought someone more experienced than me would have seen this before.

Thanks

April 20th, 2015 8:11pm

It's hard to know with out seeing your data but I am guessing it is to do with there being a time component in your date fields.  In the T-SQL for your report trying casting the date fields to date.  

IE:  CAST(DateTimeField as DATE)

This will remove the time component and hopefully give you a more consistent result.

Free Windows Admin Tool Kit Click here and download it now
April 20th, 2015 8:37pm

Ok, so I found out a few things.  It's a datetime field, so I was able to clear out the timestamp using the code below.  This is giving me better results now.  The start date is perfect, but I'm still not getting results that end on the end date.  Do I have to do a "+1" somehow?  This seems more complex than it should be.

DATEADD(dd, DATEDIFF(dd, 0, datefield, 0)


  • Edited by nd2000 3 hours 4 minutes ago update code snippit
April 21st, 2015 12:03am

My solution should work perfectly regardless.  Can you post the date data? 

CAST(DateTimeField as DATE) should achieve exactly the same as your code above - it is just simpler...

Free Windows Admin Tool Kit Click here and download it now
April 21st, 2015 12:22am

Yes 

The safest way to write a date range comparison would be as below

WHERE DateField >= @StartDate
AND DateField < DATEADD(dd,1,@EndDate)

If you want results with @EndDate inclusive

see more details here

http://visakhm.blogspot.ae/2012/12/different-ways-to-implement-date-range.html

April 21st, 2015 1:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics