Carlos Alberto
Carlos Alberto

Reputation: 1

Extract the right year, month, and day MySQL

Hi everyone I have a date data like this 23/01/22 (dd/mm/yy), I want to extract the year with EXTRACT(YEAR from date) it returns the value of 2023 instead of 2022. Does anyone know how to solve this?

Upvotes: 0

Views: 37

Answers (1)

Ergest Basha
Ergest Basha

Reputation: 9038

Always use proper date datatype.

Workaround:

select EXTRACT(YEAR from str_to_date('23/01/22','%d/%m/%y')) ;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ef455c01586b5b0cee2d13b49dc2cc56

Upvotes: 2

Related Questions