title: SQL Server : setting two variables month and year for previous month, year roll over safe
body: <p>I'm trying to basically dynamically set two variables (@month and @year) to be whatever the previous month and year were.</p>
  
  <p>So in today's case <code>@month = 7</code> and <code>@year = 2012</code></p>
  
  <p>But I want something safe for the year roll around, so if it's <code>1/1/2013</code> I would get <code>@month = 12</code> and <code>@year = 2012</code>.</p>
  
  <p>Thanks!</p>
  
  <p>This is what I have.</p>
  
  <pre><code>declare @month              int
  declare @year               int
  
  set @month = month (getDate ())-1
  set @year = 2012
  </code></pre>