views:

40

answers:

2

I need to calculate the difference between two dates (facility_start_date, facility_end_date) for a report in Reporting Services in SQL 2005. If the facility_end_date is null then it needs to use the report parameter @EndDate in the calculation. However if the facility_end_date is greater than the parameter @EndDate, then it also needs to use the paramenter @EndDate. The code below works fine except that if the facility_end_date is greater than the parameter @EndDate it is still calculating between the facility_start_date and facility_end_date, rather than between the facility_start_date and @EndDate. Any help would be appreciated.

CASE WHEN facility_start_date > facility_end_date THEN 
    NULL 
WHEN DATEPART(day , facility_start_date) > DATEPART(day , facility_end_date) THEN 
    DATEDIFF(d , facility_start_date , ISNULL(facility_end_date , @EndDate)) - 1 
WHEN DATEPART(day , .facility_end_date) > DATEPART(day , @EndDate) THEN 
    DATEDIFF(d , facility_start_date , @EndDate) - 1 
ELSE DATEDIFF(d , facility_start_date , ISNULL facility_end_date , @EndDate)) 
END
+1  A: 
   CASE
     WHEN rpt_critical_info_view.facility_start_date > rpt_critical_info_view.facility_end_date
     THEN NULL
     WHEN rpt_critical_info_view.facility_end_date IS NULL
           OR Datepart(DAY, rpt_critical_info_view.facility_end_date) > Datepart(DAY, @EndDate)
     THEN Datediff(d, rpt_critical_info_view.facility_start_date, @EndDate) - 1
     ELSE Datediff(d, rpt_critical_info_view.facility_start_date, rpt_critical_info_view.facility_end_date)
   END

I am not 100% clear on what you expect the result to be. You are evaluating DAY offsets, not date offsets. With your DATEPART function, 05/31/2010 will evaluate as greater than 06/16/2010. I am also not sure why you are subtracting 1 from the result of your DATEDIFF. If you are truly trying to calculate the DATE differences, use this:

   CASE
     WHEN rpt_critical_info_view.facility_start_date > rpt_critical_info_view.facility_end_date
     THEN NULL
     WHEN rpt_critical_info_view.facility_end_date IS NULL
           OR rpt_critical_info_view.facility_end_date > Datepart(DAY, @EndDate)
     THEN Datediff(d, rpt_critical_info_view.facility_start_date, @EndDate) - 1
     ELSE Datediff(d, rpt_critical_info_view.facility_start_date, rpt_critical_info_view.facility_end_date)
   END
Kenneth
Thanks for the responses. I am calculating the number of days between dates. I do know about table aliases and probably should have used those. I just copied the code I was using. I'm using the CASE WHEN statement within a SELECT statement so maybe there is something within that statement that is not allowing for correct calculations. With Kenneth's code I'm still not getting correct calculations if the facility end date is later than the parameter @EndDate. It calculates the days between the facility start and end date. Gourav's answer gave me a function argument count error. Thanks anyway.
Anne C
Take a look at edit.
Kenneth
This is the code that ended up working for me:CASE WHEN t1.end_date IS NULL THEN DATEDIFF(day , t1.start_date , @EndDate) WHEN t1.end_date > @EndDate THEN DATEDIFF(d , t1.start_date , @EndDate) ELSE DATEDIFF(d , t1.start_date , t1.end_date) ENDKenneth, thanks for showing me how my code could be shorter and more concise. Thanks Mark for letting me know to use table aliases to make the question more clear. I appreciate the positive feedback.
Anne C
A: 

I'm not very much clear with the problem you have and the query you posted, but see if this might help you..Use this in your select statement..

datediff(facility_start_date ,CASE WHEN facility_end_date is null 
THEN CASE WHEN facility_end_date > @EndDate THEN @EndDate ELSE @EndDate 
END ELSE facility_end_date END)
Gourav C