Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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
Author
Message
Posted Thursday, February 2, 2012 11:19 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 1,893, Visits: 3,932
Hi

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.
   [Microsoft.SqlServer.Server.SqlFunction]
[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);
sb.Append("0x");

for (int i = 0; i < buffer.Length; i++) {
byte b = buffer[i];
if (b < 0x10) {
sb.Append("0");
sb.Append(b.ToString("x"));
}
else
sb.Append(b.ToString("x"));
}

return new SqlString(sb.ToString());
}


Hope this helps
Flo



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: Today @ 7:58 AM
Points: 217, Visits: 891
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
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse