日期:2014-05-17  浏览次数:21804 次

sql语句得到近五年的年份
求一个sql语句,可以从oracle的dual表中得到近五年的年份值,即查询结果为
2008
2009
2010
2011
2012



------解决方案--------------------
SQL code

SELECT to_char(SYSDATE,'YYYY') - LEVEL + 1 years
    FROM dual
    connect by LEVEL <= 5
    ORDER BY 1

------解决方案--------------------
LS的是转换先成年了 再计算 也可以先计算再转成年

SQL code

select to_char(add_months(sysdate,-12*(level-1)),'yyyy') yy
from dual
connect by level<=5

------解决方案--------------------
select to_char(sysdate,'yyyy') from dual
union
select to_char(sysdate-365,'yyyy') from dual
union
select to_char(sysdate-365*2,'yyyy') from dual
union
select to_char(sysdate-365*3,'yyyy') from dual
union
select to_char(sysdate-365*4,'yyyy') from dual
------解决方案--------------------
探讨

select to_char(sysdate,'yyyy') from dual
union
select to_char(sysdate-365,'yyyy') from dual
union
select to_char(sysdate-365*2,'yyyy') from dual
union
select to_char(sysdate-365*3,'yyyy') from ……