How to select columns in which years are equal in Oracle?

Asked

Viewed 69 times

2

I have a table called Clientes, and in it I have the following columns :

id_cliente
nm_cliente
dt_nascimento

I need to select and count all customers who were born in the same year. I have tried some things, but they do not return on the basis of the same year but of the same day, month and year.

My current query that is not working is like this :

SELECT dt_nascimento, count(*) FROM clientes
WHERE dt_nascimento = dt_nascimento
GROUP by dt_nascimento;

How can I select only based on the same year ?

1 answer

3


You have to extract the year from the date field with the function Extract, can use this query:

SELECT  EXTRACT(year FROM dt_nascimento) as ano, count(*) as quantidade
FROM clientes
GROUP by EXTRACT(year FROM dt_nascimento);

See the result:

inserir a descrição da imagem aqui

  • Hello, thank you so much for the reply. It seems to work but is returning to me that I am missing an expression.

  • Strange, for both he said they are invalid formats. Why is this happening ?

  • 1

    It worked right now, +1 and right answer. Thank you for your patience in correcting the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.