当前位置: 代码迷 >> Sql Server >> 求个 SQL 话语,年、月相关
  详细解决方案

求个 SQL 话语,年、月相关

热度:4   发布时间:2016-04-27 12:17:08.0
求个 SQL 语句,年、月相关
1、数据库有两个字段:【年】、【月】,都是 int 类型
2、我想得到: >= 2010年5月,并且 <= 2011年12月


------解决方案--------------------
这个可以直接用条件过滤啊
大于等于2010年5月 条件就是 ((年份=2010 and 月份>=5) or 年份>2010) and (相同逻辑 <=2011年12月)
------解决方案--------------------
SQL code
--虚拟临时表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 tb1where 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        124           2010        55           2010        7(3 row(s) affected*/
  相关解决方案