NOTE: This question was asked and answered prior to SQL Server 2016 being released which introduced the COMPRESS and DECOMPRESS built-in functions (which merely expose the Compress
/ Decompress
operations of the .NET GZipStream
class). Try those first if you are using SQL Server 2016 or newer.
To uncompress the VARBINARY
value in T-SQL (whether in SSMS or a SQL script, etc), you can use a SQLCLR function to do that translation via a simple SELECT
statement. You would use the GzipStream
class that @Doug mentioned.
But, if you don't want to write any code to get this functionality, it is available in the Free version of the SQL# SQLCLR library (which I am the author of, and again, this function is free). For example:
SELECT SQL#.Util_GUnzip(PulledContent)
FROM SchemaName.TableName
WHERE SomeField = something;
That will return a VARBINARY
value as well, but it will be the uncompressed value. At that point, you can attempt to convert it to various forms to see what it really is. A binary file will still be binary, but plain text should come out as at least partially readable (fully readable if the collation is the proper encoding):
SELECT CONVERT(VARCHAR(MAX), SQL#.Util_GUnzip(PulledContent))
FROM SchemaName.TableName
WHERE SomeField = something;
or:
SELECT CONVERT(NVARCHAR(MAX), SQL#.Util_GUnzip(PulledContent))
FROM SchemaName.TableName
WHERE SomeField = something;