Home > mysql, Tips > Function to format bytes in human readable format – MySQL

Function to format bytes in human readable format – MySQL

January 7th, 2012 Leave a comment Go to comments

Just searched for a similar one, and could not find any, converted one such function which I had in hand, and is posted here Function to format bytes in human readable format, which was in php.

DELIMITER $$
DROP FUNCTION IF EXISTS `humansize`$$
DETERMINISTIC: CREATE FUNCTION `humansize` (ibytes bigint) RETURNS text DETERMINISTIC 
BEGIN
 if ibytes <= 1000 then
   return concat(ibytes, ' B');
 else
   if ibytes <= (1000* 1000) then
     return concat(ibytes / 1000, ' KB');
   else
     if ibytes < (1000* 1000 * 1000) then
       return concat(ibytes / (1000 * 1000), ' MB');
     else
       if ibytes < (1000* 1000 * 1000 * 1000) then
          return concat(ibytes / (1000 * 1000 * 1000), ' GB');
       else
          return concat(ibytes / (1000 * 1000 * 1000 * 1000), ' TB');
       end if;
     end if;
   end if;
 end if;
END$$
DELIMITER ;

Categories: mysql, Tips Tags:
  1. Rutger
    January 13th, 2012 at 01:20 | #1

    Nice! I had to add DETERMINISTIC:

    CREATE FUNCTION `humansize` (ibytes bigint) RETURNS text DETERMINISTIC

  2. January 27th, 2012 at 08:29 | #2

    Great and Thanks, happy that some one has also used this.

  1. No trackbacks yet.

eighty five − = seventy seven