DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticketSplitCounter`(vTicketFk INT) RETURNS varchar(15) CHARSET utf8mb3 COLLATE utf8mb3_general_ci NOT DETERMINISTIC READS SQL DATA BEGIN DECLARE vSplitCounter VARCHAR(15); SELECT CONCAT(printedStickers,'/',Total, IF(printedStickers = Total ,' LS','')) INTO vSplitCounter FROM ( SELECT count(l.Id_Movimiento) as printedStickers, COUNT(*) as Total FROM vn.sale s LEFT JOIN vn2008.movement_label l ON l.Id_Movimiento = s.id WHERE ticketFk = vTicketFk ) sub; RETURN IFNULL(vSplitCounter,'--'); END$$ DELIMITER ;