title | description | author | ms.author | ms.reviewer | ms.date | ms.service | ms.subservice | ms.topic | f1_keywords | helpviewer_keywords | dev_langs | monikerRange | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
COMPRESS (Transact-SQL) | The COMPRESS function compresses the input expression, using the Gzip algorithm. | markingmyname | maghan | randolphwest | 03/09/2023 | sql | t-sql | reference |
|
|
| = azuresqldb-current || = azuresqldb-mi-current || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqledge-current || = azure-sqldw-latest||=fabric |
[!INCLUDE sqlserver2016-asdb-asdbmi-asa-fabricse-fabricdw]
This function compresses the input expression, using the Gzip algorithm. The function returns a byte array of type varbinary(max).
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
COMPRESS ( expression )
An expression of one of the following data types:
- binary(n)
- char(n)
- nchar(n)
- nvarchar(max)
- nvarchar(n)
- varbinary(max)
- varbinary(n)
- varchar(max)
- varchar(n)
For more information, see Expressions (Transact-SQL).
varbinary(max), representing the compressed content of the input.
Compressed data can't be indexed.
The COMPRESS
function compresses the input expression data. You must invoke this function for each data section to compress. For more information about automatic data compression during storage at the row or page level, see Data Compression.
This example shows how to compress data inserted into a table:
INSERT INTO player ( name, surname, info ) VALUES ( N'Ovidiu', N'Cracium', COMPRESS(N'{"sport":"Tennis","age": 28,"rank":1,"points":15258, turn":17}') ); INSERT INTO player ( name, surname, info ) VALUES ( N'Michael', N'Raheem', COMPRESS(@info) );
This statement first deletes old player records from the player
table. To save space, it then stores the records in the inactivePlayer
table, in a compressed format.
DELETEFROM player OUTPUT deleted.id, deleted.name, deleted.surname, deleted.datemodifier, COMPRESS(deleted.info) INTO dbo.inactivePlayersWHERE datemodified < @startOfYear;