compress() function

The compress() function compresses a varchar or nvarchar value by using the public source zlib software library. The zlib library uses the DEFLATE compression algorithm, a variation of Lempel-Ziv 1977 (LZ77).

Compression is the process of encoding data so that it uses fewer bits. For example, compression replaces instances of contiguous, repeated characters with a single character and a count. You must decompress compressed data before you can use it.

Important: To process nvarchar values, you should use the compress_nvarchar() function instead of the compress() function and the decompress_nvarchar() function instead of the decompress() function. The compress() function remains for compatibility with earlier versions and results, but tables and views that store compress('NVARCHAR') function values might encounter problems during loads and GENERATE STATISTICS operations.

Syntax

The compress() function has the following syntax:
varchar = compress(varchar input[, int level]);
nvarchar = compress(nvarchar input[, int level]);

The input value specifies the varchar or nvarchar value to compress.

The level value specifies the compression level that is used. It can be 0 - 9, with 0 indicating the least compression and 9 indicating the most compression. The default is 6. Increasing the compression level increases the processing time.

Returns

The function returns the compressed value.

Example

select compress('1234567890');
   COMPRESS
--------------
 x3426153·˚4
(1 row)