Open In App

MySQL | COMPRESS( ) Function

The MySQL COMPRESS() function is used for the compression of a string. The value returned by the COMPRESS() function is a binary string. The COMPRESS() function stores non-empty strings as a four-byte length of the uncompressed string, which is then followed by the compressed string. If the string ends with space, a “.” character is added to the string. Also, it should be noted that empty strings are stored as empty strings. The COMPRESS() function accepts one parameter which is the string to be compressed. Syntax:

COMPRESS(string_to_compress)

Parameters Used:



Return Value: The COMPRESS function in MySQL returns a compressed string. Supported Versions of MySQL:

Example-1: Implementing COMPRESS function on a string.



SELECT 
COMPRESS('geeksforgeeks'); 

Example-2: Implementing COMPRESS function on a string which has a combination of characters and integers.

SELECT 
COMPRESS('geeksforgeeks123'); 

Output:

\0\0\0x?KOM-?N?/JOM?.642\06?? 

Example-3: Implementing COMPRESS function on a string and returning the length of the string after compression.

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

Output:

\0\0\0x?KOM?.N?/J?\0%?f    22 

Example-4: Implementing COMPRESS function on a NULL string and returning the length of the string after compression.

SELECT 
COMPRESS(NULL), LENGTH(COMPRESS(NULL)); 

Output:

NULL NULL 
Article Tags :
SQL