memaxt memaxt - 4 months ago 12
MySQL Question

MYSQL Case When simply not working

I'm trying to convert my TIMESTAMP to a string that represents the financial year period e.g. April to March

Here is my following statement:

SELECT Year, Type
CASE
WHEN date_format(SubmissionDate, '%Y')='2015' AND MONTH(SubmissionDate)
BETWEEN 4 AND 12 THEN '15-16'
WHEN date_format(SubmissionDate, '%Y')='2016' AND MONTH(SubmissionDate)
BETWEEN 1 AND 3 THEN '15-16'
END as Year
FROM Incidents


Obviously it fails...I pretty knew when it comes to CASE statements, am I anywhere near?

Answer

You've missed a , after Type:

SELECT Year, Type,
CASE 
WHEN date_format(SubmissionDate, '%Y')='2015' AND MONTH(SubmissionDate)
BETWEEN 4 AND 12 THEN '15-16'
WHEN date_format(SubmissionDate, '%Y')='2016' AND MONTH(SubmissionDate)
BETWEEN 1 AND 3 THEN '15-16'
END as Year
FROM Incidents