DELIMITER $$
CREATE DEFINER=`bacula`@`%` FUNCTION `convert_bytes_to_humanreadable`(bytes FLOAT) RETURNS varchar(20) CHARSET utf8
BEGIN

DECLARE n INT DEFAULT 1;

LOOP
    IF bytes < 1024 THEN
        RETURN concat(round(bytes, 2), ' ', elt(n, 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB'));
    END IF;
    SET bytes = bytes / 1024;
    SET n = n + 1;
END LOOP;

END$$
DELIMITER ;