Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT date range with year and month fields
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

United Kingdom
9 Posts

Posted - 06/13/2013 :  10:32:26  Show Profile  Reply with Quote

I want to select data between two date ranges where the date values are held in separate year and month columns. My current query fails because it excludes (2011,12) and (2012,1).

Any help would be greatly appreciated.


WHERE (year >= '2011' and month >= '2')
AND (year <= '2012' AND month <= '11')

--Create the test data.

create table tbDates (year int, month int)

insert into tbdates values

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 06/13/2013 :  10:41:02  Show Profile  Reply with Quote
If you are on SQL 2012 use this:

   DATEFROMPARTS(year,month,1) >= '20110201'
   AND DATEFROMPARTS(year,month,1) <= '20121101'
If you are on an earlier version of SQL, replace the left side of each comparison operator with dateadd(mm,month-1,dateadd(yy,year-1900,0))

Edited by - James K on 06/13/2013 10:41:23
Go to Top of Page

Starting Member

United Kingdom
9 Posts

Posted - 06/13/2013 :  10:56:33  Show Profile  Reply with Quote
Thanks James

Yes that works and solves my problem! Very grateful.



Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000