77

I need to add the current year as a variable in an SQL statement, how can I retrieve the current year using SQL?

i.e.

  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')

7 Answers 7

132

Using to_char:

select to_char(sysdate, 'YYYY') from dual;

In your example you can use something like:

BETWEEN trunc(sysdate, 'YEAR') 
    AND add_months(trunc(sysdate, 'YEAR'), 12)-1/24/60/60;

The comparison values are exactly what you request:

select trunc(sysdate, 'YEAR') begin_year
     , add_months(trunc(sysdate, 'YEAR'), 12)-1/24/60/60 last_second_year
from dual;

BEGIN_YEAR  LAST_SECOND_YEAR
----------- ----------------
01/01/2009  31/12/2009
2
  • 4
    Subtracking one second from the next year to get the last second of this year will work for dates and presumable timestamp with no fractional seconds. If you end up with timestamps and fractional seconds, then some values could be within the current year, and missed by the between <begin this year> and <end next year> - 1 second. Working across different RDBMSes that each have multiple date/time types with different precisions I recommend and prefer start_of_this_year <= value_under_test and value_under_test < 'start_of_next_year Apr 10, 2013 at 13:46
  • @ShannonSeverance This would be easy to misread. To be clear, FerranB's query works—there's no risk of fractional seconds messing it up. What you're describing is an alternative general solution that works with timestamps as well as dates. (I'm not saying you claimed otherwise...just underscoring that for readers who might get the wrong idea.) Your comment might work better as a separate answer since the risk you mention doesn't apply to FerranB's query. Aug 11, 2020 at 21:53
72

Another option is:

SELECT *
  FROM TABLE
 WHERE EXTRACT( YEAR FROM date_field) = EXTRACT(YEAR FROM sysdate) 
4
  • 3
    This, combined with a function based index would make the query quite fast. Jul 13, 2009 at 14:50
  • 1
    Function based index would add unneeded complexity here. Why use FBI when you can do with a simple index?
    – jva
    Jul 13, 2009 at 15:06
  • 2
    A simple index on date_field will not be used if a function (like EXTRACT) is applied to the column (tested on Oracle 11g) - would be nice if it did though. A function-based index on EXTRACT(YEAR FROM date_field) can be used, however. Jul 14, 2009 at 8:59
  • 3
    Another advantage of using a FBI in this case is that a histogram may provide the optimiser with more accurate statistics on the spread of years in the table. Jul 14, 2009 at 9:00
19

Use extract(datetime) function it's so easy, simple.

It returns year, month, day, minute, second

Example:

select extract(year from sysdate) from dual;
1
  • 1
    extract function will only extract year, month, and day from a datetime type field (like sysdate). It will extract the other fields from a timestamp field though.
    – Gerrat
    Jan 12, 2015 at 15:30
4

Yet another option would be:

SELECT * FROM mytable
 WHERE TRUNC(mydate, 'YEAR') = TRUNC(SYSDATE, 'YEAR');
1

Since we are doing this one to death - you don't have to specify a year:

select * from demo
where  somedate between to_date('01/01 00:00:00', 'DD/MM HH24:MI:SS')
                and     to_date('31/12 23:59:59', 'DD/MM HH24:MI:SS');

However the accepted answer by FerranB makes more sense if you want to specify all date values that fall within the current year.

-1

Why not use YEAR function?

SELECT * FROM table WHERE YEAR(date_field)=YEAR(SYSDATE);
-6

To display the current system date in oracle-sql

   select sysdate from dual;
1
  • 4
    Displaying the current date was not the question.
    – user330315
    Mar 11, 2014 at 7:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.