I have month names in my database, for eg. tomonth
and frommonth
. The data stored is like 'JAN', 'FEB' etc.
What do I have to do to get difference between tomonth
and frommonth
?
I have month names in my database, for eg. tomonth
and frommonth
. The data stored is like 'JAN', 'FEB' etc.
What do I have to do to get difference between tomonth
and frommonth
?
Try like below query
select (tomonth-frommonth) as monthdiff from
( SELECT case when `frommonth` = 'Jan' then 1
when frommonth = 'Feb' then 2
when frommonth = 'Mar' then 3
when frommonth = 'Apr' then 4
when frommonth = 'May' then 5
when frommonth = 'Jun' then 6
when frommonth = 'Jul' then 7
when frommonth = 'Aug' then 8
when frommonth = 'Sep' then 9
when frommonth = 'Oct' then 10
when frommonth = 'Nov' then 11
when frommonth = 'Dec' then 12
end frommonth,
( case when tomonth = 'Jan' then 1
when tomonth = 'Feb' then 2
when tomonth = 'Mar' then 3
when tomonth = 'Apr' then 4
when tomonth = 'May' then 5
when tomonth = 'Jun' then 6
when tomonth = 'Jul' then 7
when tomonth = 'Aug' then 8
when tomonth = 'Sep' then 9
when tomonth = 'Oct' then 10
when tomonth = 'Nov' then 11
when tomonth = 'Dec' then 12
end ) as tomonth from table) a
Your could use STR_TO_DATE:
SELECT
MONTH(STR_TO_DATE(tomonth,'%b'))-MONTH(STR_TO_DATE(frommonth,'%b')) as montdiff
FROM
yourtable