Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current year using SQL on Oracle?

Tags:

date

sql

oracle

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') 
like image 202
Craig Angus Avatar asked Jul 13 '09 14:07

Craig Angus


People also ask

How do I get the current year in SQL query?

MySQL YEAR() Function The YEAR() function returns the year part for a given date (a number from 1000 to 9999).

Is there a year function in Oracle?

The YEAR function returns the year part of a value. The argument must be a date, timestamp, or a valid character string representation of a date or timestamp. The result of the function is an integer between 1 and 9 999.

How do I get the year from a date in SQL Developer?

You can extract YEAR, MONTH, DAY from a DATE value by using the EXTRACT() function. The following example extracts the value of the YEAR field from a DATE value. In this example, we used the TO_DATE() function to convert a date literal to a DATE value.


1 Answers

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 
like image 138
FerranB Avatar answered Oct 07 '22 15:10

FerranB