Search code examples
sqlsql-serversql-viewdateadd

sql server dateadd() using column name year and not keyword 'year'


I am using DATEADD(year, -3, GETDATE()) to pull the last thee years worth of data on a rolling period.

I also have a column called Year, the view I am creating is using the MyTable.Year and not the sql keyword 'year'.

e.g DATEADD(MyTable.year, -3, GETDATE())

It resolves it every time in the view which is really annoying. I'm a bit rusty, been out of this for about 4 years.

How do I make sure it uses the keyword 'year', I find it strange it is doing this. Any explanation on this would also be helpful. SQL Server 2016

Thanks guys.

enter image description here

enter image description here EDIT: I have edited my misplaced schema notation and identified the table name, sorry for confusion


Solution

  • It seems your editor has problems with the keyword YEAR and replaces it with a value from a column that is also called year

    This can be solved by using a synonym for the keyword year in the DateAdd function.
    So instead of

    dateadd(year, -3, getdate())
    

    use

    dateadd(yy, -3, getdate())