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?

closed as off-topic by Philᵀᴹ, mustaccio, MDCCL, Tom V, Michael Green Nov 11 '16 at 8:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – Philᵀᴹ, MDCCL, Tom V, Michael Green
If this question can be reworded to fit the rules in the help center, please edit the question.


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;
  • 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 '16 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 '16 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 '16 at 22:45

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.