Skip to main content

COMPRESS

Description

The COMPRESS function compresses a string into binary data using the zlib compression algorithm. The compressed data can be decompressed back to the original string using the UNCOMPRESS function.

Syntax

COMPRESS(<str>)

Parameters

ParameterDescription
<str>The string to be compressed. Type: VARCHAR

Return Value

Returns VARCHAR type, which is the compressed binary data (not human-readable).

Special cases:

  • If the parameter is NULL, returns NULL
  • If the input is an empty string '', returns an empty string ''

Examples

  1. Basic usage: compression and decompression
SELECT uncompress(compress('hello'));
+-------------------------------+
| uncompress(compress('hello')) |
+-------------------------------+
| hello |
+-------------------------------+
  1. Empty string handling
SELECT compress('');
+--------------+
| compress('') |
+--------------+
| |
+--------------+
  1. NULL value handling
SELECT compress(NULL);
+----------------+
| compress(NULL) |
+----------------+
| NULL |
+----------------+
  1. UTF-8 character test
SELECT uncompress(compress('ṭṛì'));
+----------------------------------+
| uncompress(compress('ṭṛì')) |
+----------------------------------+
| ṭṛì |
+----------------------------------+