¿Cómo obtengo el año actual usando SQL en Oracle?


Necesito agregar el año actual como una variable en una instrucción SQL, ¿cómo puedo recuperar el año actual usando SQL?

Es decir,

  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')
Author: a_horse_with_no_name, 2009-07-13

6 answers

Usando to_char:

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

En tu ejemplo puedes usar algo como:

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

Los valores de comparación son exactamente lo que usted solicita:

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
 90
Author: FerranB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-19 21:55:09

Otra opción es:

SELECT *
  FROM TABLE
 WHERE EXTRACT( YEAR FROM date_field) = EXTRACT(YEAR FROM sysdate) 
 54
Author: borjab,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-13 14:46:49

Use la función extract(datetime) es tan fácil, simple.

Devuelve año, mes, día, minuto, segundo

Ejemplo:

select extract(year from sysdate) from dual;
 14
Author: Ali Elsayed Metwally,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-09 07:27:22

Ya que estamos haciendo esto hasta la muerte-usted no tiene que especificar un año:

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

Sin embargo, la respuesta aceptada por FerranB tiene más sentido si desea especificar todos los valores de fecha que caen dentro del año en curso.

 1
Author: William Robertson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-27 19:00:28

Otra opción sería:

SELECT * FROM mytable
 WHERE TRUNC(mydate, 'YEAR') = TRUNC(SYSDATE, 'YEAR');
 0
Author: David Faber,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-09 20:11:02

Para mostrar la fecha actual del sistema en oracle-sql

   select sysdate from dual;
 -5
Author: mohan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-11 08:03:06