Get Season From Match Date

I have a table that contains a series of MatchDates, i.e:


I need to add another column for the Season the match was played in.


The Season would be 2011-12 if the Match Date was between 1-Oct-2011 to 31-Mar-2012
The Season would be 2012 if the Match Date was between 1-Apr-2012 to 30-Sep-2012

Apart from manually setting up update satements for the past 200 years, is there a quick and easy way to automate this?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

use a case statement to add on this column


CASE (yourdatecolumn)
WHEN (yourdatecolumn) BETWEEN '1-Oct-2011'  and  '31-Mar-2012 ' THEN '2011-12 '
WHEN (yourdatecolumn) BETWEEN '1-Apr-2012 ' and  '30-Sep-2012 ' THEN '2012 '
END as Season
this way you wont have to actually "update" your table
just add that case statement into your query /view/SP/Trigger  or whatever you use when your extacting that data and it will create that column on the fly whenever needed  :)
you can create a stored function
USE `my_own_database`;
DROP function IF EXISTS `get_season`;

USE `test`$$
CREATE FUNCTION `get_season`(given_date date) RETURNS varchar(10)
	declare sResult nvarchar(10);
	declare nYear integer;

	set nYear = year(given_date);

	if given_date between date(concat(nYear, '04', '01')) and date(concat(nYear, '09', '30')) then 
		set sResult = nYear;
	elseif given_date > date(concat(nYear, '09', '30')) then
		set sResult = concat(nYear, '-', (nYear+1));
		set sResult = concat((nYear-1), '-', nYear);
	end if;

RETURN sResult;


Open in new window

then call it in your query
select date_of_match, get_season(date_of_match) as season from MatchTable

Open in new window

to update your table (let's say you already have an empty string field Season)
update MatchTable set Season = get_season(date_of_match)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterErhardAuthor Commented:
Thanks so much :)
You're welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.