0

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?

2 Answers 2

0

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;
3
  • In this case the error is the name. Here is my actual syntax:DECLARE (at)NumberOfyears INT = 30 , @StartDate DATETIME = '2010-01-01'; DECLARE (at)CustOffDate DATE = DATEADD(YEAR, (at)NumberOfYears, (at)StartDate); SELECT (at)CutoffDate; (replaed at sign with (at). The comments here will not let me submit with 'at sign". But my syntax appears to be correct based on your above example
    – Joe Resler
    Nov 10, 2016 at 21:51
  • In your actual syntax posted your date variable is CustOffDate, and you are selecting CutoffDate. Typo with the extra s.
    – Nic
    Nov 10, 2016 at 22:04
  • that was it. I'm a moron. I apologize. When removing the "s" I was able to move forward. I think the difference is that I moved on to executing other statements AFTER executing the numberof years etc. I think they need to be in sequence? Either way, thank you for your help.
    – Joe Resler
    Nov 10, 2016 at 22:45
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.