I am trying to create a stored procedure that when given a month value, returns the user the last day of the month.
So far:
DROP PROCEDURE IF EXISTS getLastDayMonth$$
CREATE PROCEDURE getLastDayMonth(IN inMonth int)
BEGIN
SELECT LAST_DAY(inMonth);
END $$
I am having trouble figuring out how i can make inMonth
a parameter that the sql function Last_DAY
accepts. Is there another function that takes in an int as a month and returns the last day of the month? I am trying to avoid giving a DATE
type as a paramater
You can add moths to first year day and get last_date:
SELECT LAST_DAY( DATE_ADD( '2012-01-01 00:00:00',
INTERVAL inMonth - 1 MONTH)
);
Alternative solution is ELT function:
SELECT cast(
ELT(inmonth, '31', '28', '31', '30', '31', .... ,'31' )
as SMALLINT
) as last_day
The last one, use case
statement:
SELECT case inmonth
when 1 then 31
when 2 then 29
...
when 12 then 31
end as last_day
EDITED
mysql> CREATE PROCEDURE getLastDayMonth(IN inMonth int)
-> begin
-> SET @t=inMonth -1;
-> SELECT LAST_DAY( DATE_ADD( '2012-01-01 00:00:00',INTERVAL @t MONTH));
-> end@@
Query OK, 0 rows affected (0.24 sec)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.