SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Undoing compression


Undoing compression

Author
Message
Steve Jones
Steve Jones
SSC Guru
SSC Guru (689K reputation)SSC Guru (689K reputation)SSC Guru (689K reputation)SSC Guru (689K reputation)SSC Guru (689K reputation)SSC Guru (689K reputation)SSC Guru (689K reputation)SSC Guru (689K reputation)

Group: Administrators
Points: 689838 Visits: 21594
Comments posted to this topic are about the item Undoing compression

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSC Guru
SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)

Group: General Forum Members
Points: 70502 Visits: 8350
Nice question, thanks Steve

Compress & decompress are really handy functions...

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Carlo Romagnano
Carlo Romagnano
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21405 Visits: 3777
The return type is VARBINARY(MAX) , so, it should be CASTed to a char type.
Also, there's an undocumented function UNCOMPRESS.
select UNCOMPRESS(decompress(COMPRESS('a')))

Try: it returns the starting string.

Shayn Thomas
Shayn Thomas
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5194 Visits: 915
Cheers Steve,
I havent used these yet so led to some good reading

---------------------------------------------------------------------------------------
The more you know, the more you know that you dont know
primitivefuture2006
primitivefuture2006
SSC Veteran
SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)

Group: General Forum Members
Points: 299 Visits: 254
Carlo Romagnano - Tuesday, November 6, 2018 1:18 AM
The return type is VARBINARY(MAX) , so, it should be CASTed to a char type.
Also, there's an undocumented function UNCOMPRESS.
select UNCOMPRESS(decompress(COMPRESS('a')))

Try: it returns the starting string.

What is the difference between UNCOMPRESS() and DECOMPRESS()?

Why even both using COMPRESS(), DECOMPRESS(), or UNCOMPRESS()?

Thanks

Carlo Romagnano
Carlo Romagnano
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21405 Visits: 3777
primitivefuture2006 - Tuesday, November 6, 2018 6:01 AM
Carlo Romagnano - Tuesday, November 6, 2018 1:18 AM
The return type is VARBINARY(MAX) , so, it should be CASTed to a char type.
Also, there's an undocumented function UNCOMPRESS.
select UNCOMPRESS(decompress(COMPRESS('a')))

Try: it returns the starting string.

What is the difference between UNCOMPRESS() and DECOMPRESS()?

Why even both using COMPRESS(), DECOMPRESS(), or UNCOMPRESS()?

Thanks


UNCOMPRESS seems CASTing VARBINARY(MAX) to a char type.
string -> COMPRESS -> VARBINARY(MAX) -> DECOMPRESS -> VARBINARY(MAX) -> UNCOMPRESS -> string
Solomon Rutzky
Solomon Rutzky
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15131 Visits: 3541
Carlo Romagnano - Tuesday, November 6, 2018 6:13 AM


UNCOMPRESS seems CASTing VARBINARY(MAX) to a char type.
string -> COMPRESS -> VARBINARY(MAX) -> DECOMPRESS -> VARBINARY(MAX) -> UNCOMPRESS -> string

I wouldn't exactly recommend using UNCOMPRESS as it seems to only work for VARCHAR, not NVARCHAR:

SELECT UNCOMPRESS(CONVERT(VARBINARY(MAX), 'Hello')),
UNCOMPRESS(CONVERT(VARBINARY(MAX), N'Bye'));
-- Hello B

SELECT CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), 'Hello')),
CONVERT(NVARCHAR(MAX), CONVERT(VARBINARY(MAX), N'Bye'));
-- Hello Bye


As you can see, the UNCOMPRESS in the first query, when given Unicode data, cuts off after the first character because the "B" is 0x4200 instead of just 0x42. And the 0x00 is the null-terminator for strings, so it stops there.

It also seems kinda silly that UNCOMPRESS even exists given that it only saves typing of 1 word: UNCOMPRESS vs CONVERT(VARCHAR(...).

Take care, Solomon..


SQL# - https://SQLsharp.com/ ( SQLCLR library of over 340 Functions and Procedures)
Sql Quantum Lift - https://SqlQuantumLift.com/ ( company )
Sql Quantum Leap - https://SqlQuantumLeap.com/ ( blog )
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search