Skip to main content
Converts a byte count into a human-readable string with appropriate units (B, KB, MB, GB, TB, PB, EB). This function is useful for displaying file sizes, table sizes, memory usage, and other storage-related metrics in a user-friendly format. It uses 1024-based calculations (binary prefixes) but displays units as KB, MB, GB for simplicity and familiarity. This function is supported from v3.4.

Syntax

Parameters

  • bytes: the number of bytes to format. BIGINT is supported. Must be a non-negative integer value.

Return value

Returns a VARCHAR value representing the formatted byte size with appropriate units.
  • For values less than 1024: returns the exact number with “B” (bytes)
  • For larger values: returns a formatted string with 2 decimal places and the appropriate unit (KB, MB, GB, TB, PB, EB)
  • Returns NULL if the input is negative or NULL

Usage notes

  • The function uses 1024-based (binary) calculations internally (1 KB = 1024 bytes, 1 MB = 1024² bytes, etc.)
  • Units are displayed as KB, MB, GB, TB, PB, EB for user familiarity, though they represent binary prefixes (KiB, MiB, GiB, etc.)
  • Values greater than or equal to 1 KB are displayed with exactly 2 decimal places
  • Byte values (less than 1024) are displayed as whole numbers without decimal places
  • Negative values return NULL
  • Supports values up to 8 EB (exabytes), covering the full range of BIGINT

Examples

Example 1: Format various byte sizes.
Example 2: Handle edge cases and null values.
Example 3: Practical usage for table size monitoring.
Example 4: Precision and rounding behavior.
Example 5: Full range of supported units.

keyword

FORMAT_BYTES