How can I get the date of last friday of march month using current month with mysql 8 version?
Thanks in advance.
My code below… the return is not the last Friday of March, but the last Tuesday of March
SELECT
CASE WHEN MONTH(CURDATE()) = 1 THEN
DATE_ADD(DATE_FORMAT(LAST_DAY(NOW()) - ((7 + WEEKDAY(LAST_DAY(NOW())) - 4) % 7), '%Y-%m-%d'), INTERVAL 2 MONTH) ELSE NULL END last_friday_march_month;
+-------------------------+
| last_friday_march_month |
+-------------------------+
| 2024-03-26 |
+-------------------------+
1 row in set (0.06 sec)
2
Answers
The only thing you need from the current date is a year, so I’d suggest writing it like this:
using a recursive cte