You can, of course, like myself in the past look up how these things are done every time you need them. But this gets frustrating and it’s often easier to surround yourself with your set of own SQL-functions that give you what you need and are easy to remember.
For this purpose I give you:
- First of the month date:
- Last of the month date:
drop function if exists fn_getLastOfMonth;
create function fn_getLastOfMonth(date_ datetime) returns datetime
begin
set @dater = date_;
set @dater = DATE_FORMAT(last_day(@dater) ,’%Y-%m-%d’);
return @dater;
end - Days in the month:
drop function if exists fn_getDaysInMonth;
create function fn_getDaysInMonth(date_ datetime) returns int
begin
set @dater = date_;
set @dater = day(last_day(@dater));
return @dater;
end
drop function if exists fn_getFirstOfMonth;
create function fn_getFirstOfMonth(date_ datetime) returns datetime
begin
set @dater = date_;
set @dater = DATE_FORMAT(@dater ,’%Y-%m-01′);
return @dater;
end
Do let me know if there are other MySQL date and time functions that you would like to know.
Happy to post them here…