这个可以直接用条件过滤啊 大于等于2010年5月 条件就是 ((年份=2010 and 月份>=5) or 年份>2010) and (相同逻辑 <=2011年12月)
将年月两个字段相加,SELECT [year]+[month] FROM TableName 得到如201104 这样的,后用CONVERT 函数转换一下。希望对你有帮助
--虚拟临时表 with tb1 as ( select 1 as id ,2009 as v_year,5 as v_month union all select 2,2010,12 union all select 3,2011,2 union all select 4,2010,5 union select 5,2010,7 ) select * from tb1 where v_year>=year('2010-05-01') and v_month>=MONTH('2010-05-01') and v_year<=year('2011-12-01') and v_month<=MONTH('2011-12-01') /* id v_year v_month ----------- ----------- ----------- 2 2010 12 4 2010 5 5 2010 7(3 row(s) affected */
[code=SQL] insert into # tab1 select tab2.* from tab2 where year>='2010' and month>'05'select * from tab1 where year<='2011' and month<='12' [code]
大于等于2010年5月 条件就是 ((年份=2010 and 月份>=5) or 年份>2010) and (相同逻辑 <=2011年12月)
with tb1 as
(
select 1 as id ,2009 as v_year,5 as v_month union all
select 2,2010,12 union all
select 3,2011,2 union all
select 4,2010,5 union
select 5,2010,7
)
select * from tb1
where v_year>=year('2010-05-01') and v_month>=MONTH('2010-05-01')
and
v_year<=year('2011-12-01') and v_month<=MONTH('2011-12-01')
/*
id v_year v_month
----------- ----------- -----------
2 2010 12
4 2010 5
5 2010 7(3 row(s) affected
*/
insert into # tab1
select tab2.* from tab2 where year>='2010' and month>'05'select * from tab1 where year<='2011' and month<='12'
[code]
http://blog.csdn.net/dba_huangzj/article/details/7657979这篇是完整版