Thomas Williams Thomas Williams - 2 years ago 106
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.


,(getpreviousAprilDate as month) FROM

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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download