Issue with DECLARE @CutOffDate DATE = DATEADD(YEAR), @NumberOfYears, @StartDate); [closed]
-
04-10-2020 - |
Question
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?
Solution
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;
OTHER TIPS
I notice a case-sensitive difference between your declaration @CutoffDate
, and @CutOffDate
from the error message.
Check to see if you're not using a case-sensitive collation.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange