Using ISNULL with Dates

  • Hi everyone

    I am using the following code to calculate the difference between 2 dates:

    CASE ISNULL(DATE_1,'')

    WHEN '' THEN DATEDIFF(dd,DATE_2,DATE_3)

    ELSE DATEDIFF(dd,DATE_2,DATE_1)

    END

    Essentially if the DATE_1 field is blank then the date difference should be between the DATE_2 field and the DATE_3 field. However, if the DATE_1 field is populated then the difference should be between the DATE_2 field again and the DATE_1 field. This works perfectly well but we have now included a new date field in our DB and i need to incorporate this into the calculation as well. If the new DATE_4 field is blank then we should use the same logic as before (either the DATE_2 and DATE_1 field or if DATE_1 is blank then the DATE_2/DATE_3 fields. But if the DATE_4 field IS populated then the difference should be between the DATE_2 and DATE_4 field.

    I'm pretty sure this is quite simple but i'm getting tied up in knots!

    Any help greatly appreciated.

  • You should be using coalesce. I'm not sure if I understand exactly what you want but try something like this:

    datediff(dd, DATE_2, coalesce(DATE_4,DATE_1,DATE_3))

    The probability of survival is inversely proportional to the angle of arrival.

  • Thanks so much - i'll give that a try. 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply