--- title: "COMPRESS (Transact-SQL) | Microsoft Docs" ms.custom: "" ms.date: "07/24/2017" ms.prod: sql ms.prod_service: "database-engine, sql-database" ms.component: "t-sql|functions" ms.reviewer: "" ms.suite: "sql" ms.technology: t-sql ms.tgt_pltfrm: "" ms.topic: "reference" f1_keywords: - "COMPRESS" - "COMPRESS_TSQL" helpviewer_keywords: - "COMPRESS function" ms.assetid: c2bfe9b8-57a4-48b4-b028-e1a3ed5ece88 caps.latest.revision: 9 author: edmacauley ms.author: edmaca manager: craigg --- # COMPRESS (Transact-SQL) [!INCLUDE[tsql-appliesto-ss2016-asdb-xxxx-xxx-md](../../includes/tsql-appliesto-ss2016-asdb-xxxx-xxx-md.md)] This function compresses the input expression, using the GZIP algorithm. The function returns a byte array of type **varbinary(max)**. ![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) ## Syntax ```sql COMPRESS ( expression ) ``` ## Arguments *expression* A * **binary(***n***)** * **char(***n***)** * **nchar(***n***)** * **nvarchar(max)** * **nvarchar(***n***)** * **varbinary(max)** * **varbinary(***n***)** * **varchar(max)** or * **varchar(***n***)** expression. See [Expressions (Transact-SQL)](../../t-sql/language-elements/expressions-transact-sql.md) for more information. ## Return types **varbinary(max)** representing the compressed content of the input. ## Remarks Compressed data cannot be indexed. The `COMPRESS` function compresses the input expression data. You must invoke this function for each data section to compress. See [Data Compression](../../relational-databases/data-compression/data-compression.md) for more information about automatic data compression during storage at the row or page level. ## Examples ### A. Compress Data During the Table Insert This example shows how to compress data inserted into a table: ```sql 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)); ``` ### B. Archive compressed version of deleted rows 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. ```sql DELETE player WHERE datemodified < @startOfYear OUTPUT id, name, surname datemodifier, COMPRESS(info) INTO dbo.inactivePlayers ; ``` ## See also [String Functions (Transact-SQL)](../../t-sql/functions/string-functions-transact-sql.md) [DECOMPRESS (Transact-SQL)](../../t-sql/functions/decompress-transact-sql.md)