 DELIMITER $$
DROP FUNCTION IF EXISTS _H_to_STR$$
CREATE DEFINER=`root`@`localhost` FUNCTION _H_to_STR(
	_HOURS DOUBLE(16,4),_MODE VARCHAR(4)
) RETURNS VARCHAR(30)
    NO SQL
    DETERMINISTIC
BEGIN
	SET @.sign=_HOURS<0,
		_HOURS=ABS(IF(_MODE='m',ROUND(_HOURS*60)/60,_HOURS)),
		_HOURS=ABS(IF(_MODE='h',ROUND(_HOURS),_HOURS));
	
	SET @.days=TRUNCATE(_HOURS/24,0),@.hours=TRUNCATE(MOD(_HOURS,24),0),
		@.mins=IF(_MODE IN ('h'),0,TRUNCATE(MOD(_HOURS*60,60),0)),
		@.secs=TRUNCATE(MOD(_HOURS*3600,60),0);
		IF _MODE IN ('h','m') THEN 
			SET @.mins=@.mins+(@.secs>29);
			SET @.secs=0; 
		END IF;
		IF _MODE IN ('h') THEN 
			SET @.hours=@.hours+(@.mins>29);
			SET @.mins=0;
		END IF;
	SET @.Str=TRIM(CONCAT(
		IF(@.sign,'- ',''),
		IF(@.days>0,CONCAT(FORMAT(@.days,0),' dni '),''),
		IF(@.hours>0,CONCAT(FORMAT(@.hours,0),' godz '),''),
		IF(@.mins>0,CONCAT(FORMAT(@.mins,0),' min '),''),
		IF(@.secs>0,CONCAT(FORMAT(@.secs,0),' sek '),'')
	));
	
	RETURN IF(@.Str='','0 godz',@.Str);
END $$
DELIMITER ;