GETDATE - number of dates - a column / division in SQL

  • I need to do such a calculation.

    (Get today's date - 219 - HISTSTARTDATE) / 7 AS 'Weeks Of History'

    When I do it like this

    (DATEDIFF(DAY,0,GETDATE()) - DATEADD(DAY, -219, GETDATE()) - HISTSTART) / 7 AS 'Weeks Of History'

     

    I am getting an error which says 'The data types datetime and date are incompatible in the subtract operator.'

    I am pretty confused because I believe it was working for me before. Does someone may have an idea what can be wrong and how to fix it?

    Please let me know if you know.

     

  • DATEDIFF() returns an integer value. DATEADD() returns a date value. You can't subtract a date from a number. You can do decimal math on datetime values, though.

    SELECT DATEDIFF(week, (GETDATE() - 219), HISTSTART) AS 'Weeks Of History'

     

     

    Eddie Wuerch
    MCM: SQL

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

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