Sql-server – Issue with DECLARE @CutOffDate DATE = DATEADD(YEAR), @NumberOfYears, @StartDate);

datedimensionsql serversql-server-2012

When executing the steps in this query. I am running into an issue with the

DECLARE @CutoffDate DATE = DATEADD(YEAR, @NumberOfYears, @StartDate);

I get a message of Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable “”CutOffDate”.

Should I be adding something in this syntax? Meaning “year” should be defined? or something along those lines?

Best Answer

Provided that you have defined the @StartDate and @NumberOfYears this works just fine.

DECLARE @NumberOfYears INT = 1 ,
    @StartDate DATETIME = '2016-11-01';

DECLARE @CutoffDate DATE = DATEADD(YEAR, @NumberOfYears, @StartDate);

SELECT  @CutoffDate;