Search code examples
sql-serverdateadd

DateAdd (y, -1, getdate()) Not working


I'm trying to find all records that took place in the last year (to the day/date, not worried about time).

My SQL is:

dm.fromdatetime >= dateadd(y,-1,getdate())
and dm.fromdatetime <= getdate()

Not sure what is missing here, it seems like this should work. Suggestions?


Solution

  • Use "yy", not "y":

    dm.fromdatetime >= dateadd(yy,-1,getdate())
    and dm.fromdatetime <= getdate()
    

    As per Books Online, "y" is "dayofyear", and not year.