title: SQL Server 2008 select data only between month and year
body: <p>I would like select data between two date, without day</p>
  
  <p>An input example:</p>
  
  <p>start month: 9 , start year: 2011<br>
  end month: 3, end year: 2012</p>
  
  <p>I think that there are two way to do this.</p>
  
  <p>The first is convert start month and start year to date like <code>2011-09-01</code> and convert last date to <code>2012-03-31</code>, but this requires calculation of the last day of end month. Obtained these date we can use a BEETWEN function for the WHERE clause (but, is the CONVERT function reliable?)</p>
  
  <p>The second solution is to use the <code>DATEPART</code> function like in the following code:</p>
  
  <p>I try to explain: if end year is equal to the initial year, then month must be between the start and end months; else if the final months is greater than the initial years if different from the initial and final year, I take everything in between; else if the final year, the month must be less than or equal to the final month, if the initial year, month must be greater than or equal to the final month</p>
  
  <p>Can you help me do this in the best way? Is correct, the solution I adopted?</p>
  
  <pre><code>declare @IndDebitoCredito bit,@ProgTributo int,@mi as integer,@ai as integer,@mf as integer,@af as integer,@IDAnagrafica varchar(5)
  select @mi = 01,@ai = 2011,@mf = 12,@af = 2011,@IDAnagrafica = 'DELEL',@IndDebitoCredito = 1
  select distinct rrd.IDTributo
      from TBWH_Delega d
      --inner join TBWH_SezioneDelega sd on d.IDDelega = sd.IDDelega
      inner join TBWH_Rigo rd on rd.IDDelega = d.IDDelega
      inner join TBWH_RataRigo rrd on rrd.IDRigo = rd.IDRigo 
      where
      (
          DATEPART(MM,d.DataDelega)&lt;=@mf and 
          DATEPART(MM,d.DataDelega)&gt;=@mi and 
          DATEPART(YYYY,d.DataDelega)=@ai and 
          @af = @ai
      ) 
      OR
      (
          --anno finale magg. anno iniziale
          @af &gt; @ai AND
          ( 
              (   -- delega nell'intervallo
                  DATEPART(YYYY,d.DataDelega)&lt;@af AND 
                  DATEPART(YYYY,d.DataDelega)&gt;@ai
                  -- DATEPART(MM,d.DataDelega)&gt;=@mi 
              )
              OR
              (   -- delega limite destro
                  DATEPART(YYYY,d.DataDelega)=@af AND 
                  DATEPART(MM,d.DataDelega)&lt;=@mf 
              )
              OR
              (   -- delega limite sinistro
                  DATEPART(YYYY,d.DataDelega)=@ai AND 
                  DATEPART(MM,d.DataDelega)&gt;=@mi 
              )
          )
      )
  </code></pre>
  
  <p>GO</p>