DELIMITER // create function _first_date ( _date varchar(8) ) returns VARCHAR(8) begin declare ret varchar(8); declare pmDate datetime; set pmDate = date_add(_date, interval -day(_date)+1 day); set ret = concat(substring(pmDate, 1,4),substring(pmDate, 6,2),substring(pmDate, 9,2)); RETURN ret; end; // DELIMITER ; select _first_date('20220101')