I have created_at column(date type) I need to format it with just year and month before case when. I try this query but it gives 0 and I have users in my table!
I’m using MySQL:
select
count(case when DATE_FORMAT(created_at, '%Y-%c') = 2022-1 then 1 end)
as one from `users` limit 1
>Solution :
You have to use a string instead of the arithmetic expresseion:
select
count(case when DATE_FORMAT(created_at, '%Y-%c') = '2022-1' then 1 end)
as one from `users` limit 1
Your expression is the same as
select
count(case when DATE_FORMAT(created_at, '%Y-%c') = 2021 then 1 end)
as one from `users` limit 1