Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase ««12

Huge varbinary or image to hex string Expand / Collapse
Posted Thursday, February 2, 2012 11:19 AM



Group: General Forum Members
Last Login: Thursday, February 5, 2015 1:08 AM
Points: 1,893, Visits: 3,934

If you need to convert this size of binary data I'd either try to do this in a client application (c#/java/c++/...) or consider to use a SQL CLR function.

Just tried this one and I get a execution duration of approximately 0.5 seconds with an input buffer of 1.5 mb.
[return: SqlFacet(MaxSize = -1)]
public static SqlString fn_clr_varbintohexstring(
[SqlFacet(MaxSize=-1, IsNullable=false)]
SqlBytes binin) {

byte[] buffer = binin.Buffer;
StringBuilder sb = new StringBuilder(buffer.Length * 2 + 2);

for (int i = 0; i < buffer.Length; i++) {
byte b = buffer[i];
if (b < 0x10) {

return new SqlString(sb.ToString());

Hope this helps

The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Post #1245990
Posted Friday, February 3, 2012 4:40 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, June 6, 2016 11:50 PM
Points: 247, Visits: 1,104
Thanks for that. Though I should have come back to say I was able to optimise it by calling it with 20k chunks and putting them all back together myself... at about 1 minute per query but liveable.

However, then I discovered that neither SSMS nor SQLCMD were built to handle rows that exceed a megabyte of raw text data. Even pulling it back as XML format (which is what Microsoft recommends in this situation) was causing issues with things being truncated... and then saving it into a text file and trying to open it in notepad or SSMS? Even more problems.

Now I understand why stuff like SQL Data Compare (3rd party product) do it in few-thousand char wide column chunks with UPDATETEXT. Unfortunately, we don't own the scripting versions that might let me automate it.

Anyhow I was using it in combination with another open source stored procedure that generates INSERT statements (with this plugged in to do the IMAGE bits). It'll only take a bit more pain for me to finish it up, and hopefully I can give it back without too much arm bending from the company.
Post #1246388
Posted Wednesday, April 22, 2015 3:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 22, 2015 3:37 AM
Points: 1, Visits: 15
After use this function I retrive result without 2 chars.

When i replace code from:
SET @len = DATALENGTH(@pbinin)


SET @len = DATALENGTH(@pbinin)+1

resultset contains all chars.
Post #1679169
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse