w3resource

MySQL COMPRESS() function

COMPRESS() function

MySQL COMPRESS() function compress a string and returns the result as a binary string.

Nonempty strings are stored as a four-byte length of the uncompressed string (low byte first), followed by the compressed string. If the string ends with space, an extra "." character is added to avoid problems with end space trimming.

Empty strings are stored as empty strings.

Syntax:

COMPRESS(string_to_compress);

Argument:

Name Description
string_to_compress A string which is to be compressed.

Syntax Diagram:

MySQL COMPRESS() Function - Syntax Diagram

MySQL Version: 8.0


Example

Code:

SELECT COMPRESS('text'),LENGTH(COMPRESS('text'));

Explanation:

The above MySQL statement will compress the string text and also returns the length of the string after compression.

Output:

mysql> SELECT COMPRESS('text'),LENGTH(COMPRESS('text'));
+------------------+--------------------------+
| COMPRESS('text') | LENGTH(COMPRESS('text')) |
+------------------+--------------------------+
|    xœ+I­( gÆ |                       16 | 
+------------------+--------------------------+
1 row in set (0.03 sec)

Previous: AES_ENCRYPT()
Next: DECODE()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/encryption-and-compression-functions/compress().php