我需要在sql语句中将当前年份添加为变量,如何使用sql检索当前年份?
即:
BETWEEN TO_DATE('01/01/**currentYear** 00:00:00','DD/MM/YYYY HH24:MI:SS') AND TO_DATE('31/12/**currentYear** 23:59:59','DD/MM/YYYY HH24:MI:SS')
使用to_char:
select to_char(sysdate,'YYYY') from dual;
在你的例子中,你可以使用类似:
BETWEEN trunc(sysdate,'YEAR') AND add_months(trunc(sysdate,'YEAR'),12)-1/24/60/60;
比较值正是您要求的:
select trunc(sysdate,'YEAR') begin_year,add_months(trunc(sysdate,12)-1/24/60/60 last_second_year from dual; BEGIN_YEAR LAST_SECOND_YEAR ----------- ---------------- 01/01/2009 31/12/2009