Sunday, April 17, 2005

MySQL SELECT CASE example

select
CASE month when "01" then "January"
when "02" then "February"
when "03" then "March"
when "04" then "April"
when "05" then "May"
when "06" then "June"
when "07" then "July"
when "08" then "August"
when "09" then "September"
when "10" then "October"
when "11" then "November"
when "12" then "December"
END
from calendar where year = "2005" order by month

15 comments:

Terri said...

Thanks for the post - my reference guide was missing the END!

Romi Ardiansyah said...

Thx, i finished my work !!!

Romi Ardiansyah said...

Thx, i finished my work !!!

Sharad Garg said...

Thanks i missed "End".

Sourabh said...

Thanks so much... This example really helpful to me...

Post More example so that everyone use

Thanks
Sourabh

Anonymous said...

thanks very useful for me....bravo..

Anonymous said...

i wonder, what's so helpful about that?? it is an expected and documented behavior:

http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#operator_case

But why this construction doesn't work -

CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END

Anonymous said...

Thank you so much , it helped

nfo said...

some time ago, i saw somebody program their own date calculation like this...
please everybody, dont use this example if you just like to know which month it is !!!! mysql have good date function, which are much more comfortable..

Gourav Joshi said...

thanks...

Sreejish said...

SELECT
CASE DATE_FORMAT(createdate, '%m')
when "01" then "January"
when "02" then "February"
when "03" then "March"
when "04" then "April"
when "05" then "May"
when "06" then "June"
when "07" then "July"
when "08" then "August"
when "09" then "September"
when "10" then "October"
when "11" then "November"
when "12" then "December"
END as month,count(userid) as crsecnt
from users where DATE_FORMAT(createdate, '%Y') = "2009" group by DATE_FORMAT(createdate, '%m')

Anonymous said...

Understood the MYSQL case

Naveed said...

Thank You! You made it sooooo Easy!

Anonymous said...

nice, example. keep it up

m4nu said...

Great tip. Just what I was looking for to convert currencies on the fly. Thanks.