Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peterpan26's avatar

date field in varchar how to extract year from year_month mysql laravel

hello, i have the following field in varchar. "mes" how do i extract or get the year only from that year_month ? i tried this in multiple synthaxes and use the year() and none of them worked. " EXTRACT(YEAR_MONTH FROM datacompleta) AS mes "

0 likes
4 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

It's a varchar not a date column, so use substring

SUBSTRING(datacompleta, 0, 4) AS mes
Tray2's avatar

Why are you storing a date as varchar?

peterpan26's avatar

@Tray2 i'm not sure i haven't look much into it, but i guess it's because the blade file has a type as "month" and when i tried changing to date format on phpmyadmin, it didn't load the view, against varchar wich it did. im also using datetime-local in other fields and they work but this specific one of month i need to extract the year on a separate page.

Please or to participate in this conversation.