mysql function to pretty print sizes (pg_size_pretty equivialent)? -


postgresql have pg_size_pretty() convenience function:

> select pg_size_pretty(100000);  pg_size_pretty ----------------  98 kb 

does mysql have similar ?

if not, before make own have made this, can share ?

there no shipped function in mysql. so, created 1 : function pretty_size https://github.com/lqez/pastebin/blob/master/mysql/pretty_size.sql

create function pretty_size( size double )     returns varchar(255) deterministic begin     declare c int default 0;     declare unit int default 1000;     declare unitchar char(6) default 'kmgtpe';     declare binaryprefix boolean default 1;      /* set binaryprefix = 1 use binary unit & prefix */     /* see iec 60027-2 a.2 , iso/iec 80000 */     if binaryprefix = 1         set unit = 1024;     end if;      while size >= unit , c < 6         set size = size / unit;         set c = c + 1;     end while;      /* under 1k, add 'byte(s)' */     if c = 0         return concat( size, ' b' );     end if;      /* modify taste */     return concat(         format( size, 2 ),         ' ',         substr( unitchar, c, 1 ),         if( binaryprefix, 'ib', 'b' )         ); end $$  delimiter ; 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -