Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can format column name in case when in sql

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading