Thomas Williams Thomas Williams - 7 months ago 44
SQL Question

Mysql get date previous April 1st from a date in database

I need to run a mysql query and get a date from the database which I know how to do, but I need to do an operation which requires me to get the previous 1st April in the same query.

eg SELECT

Date
,(getpreviousAprilDate as month) FROM
sometable


Ok I will try and clarify. I need to get a date from the database in the date column. It could be any date eg 2016-02-15 or 2015-01-12 and from this I need to put in another column the previous April date. So if the date was 2015-01-12 the previous April would be 2014-04-01. I hope that makes my question clearer.

Answer

I hope this is what you want.

SELECT date,
CONCAT(IF(MONTH(date) < 4, YEAR(date) - 1, YEAR(date)), '-04-01') as month
FROM sometable