Skip to content

Instantly share code, notes, and snippets.

@thybag
Created November 7, 2017 12:14
Show Gist options
  • Save thybag/465b1955504c0f922c9b12bce5774ffc to your computer and use it in GitHub Desktop.
Save thybag/465b1955504c0f922c9b12bce5774ffc to your computer and use it in GitHub Desktop.
Get UK tax year from a date column in MYSQL

Its not pretty but it works

Stand alone

(date time being '2017-04-06')

SELECT CONCAT(year('2017-04-06' + INTERVAL -3 MONTH + INTERVAL -5 DAY),'-',YEAR('2017-04-06' + INTERVAL -3 MONTH + INTERVAL -5 DAY)+1) as tax_year

As a function

CREATE FUNCTION taxyear (d DateTime)
RETURNS CHAR(9) DETERMINISTIC
RETURN CONCAT(year(d + INTERVAL -3 MONTH + INTERVAL -5 DAY),'-',YEAR(d + INTERVAL -3 MONTH + INTERVAL -5 DAY)+1);

select TAXYEAR('2017-04-06');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment