0

This is my code:

select datepart(year,fechaingreso) as ingreso, count(*)
from empleados
group by datepart(year,fechaingreso);

And it gives me the following error:

ORA-00904: "DATEPART": identificador no válido
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error en la línea: 132, columna: 12

What or where is the error in my code please?

MT0
  • 143,790
  • 11
  • 59
  • 117

1 Answers1

0

That's because your database is Oracle (that's what error message says, ORA-...), while code you apply to access its data is (most probably MS SQL Server).

Documentation says:

The DATEPART() function returns a specified part of a date.

It seems you want to fetch year from that column. In that case, use the extract function:

SQL> create table empleados as select trunc(sysdate) fechaingreso from dual;

Table created.

SQL> select extract(year from fechaingreso) as ingreso,
  2         count(*)
  3  from empleados
  4  group by extract(year from fechaingreso);

   INGRESO   COUNT(*)
---------- ----------
      2022          1

SQL>
Littlefoot
  • 131,892
  • 15
  • 35
  • 57