Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Varbinary(max): select a subset of bytes from the varbinary field

What is the most efficient way of reading just part of the binary data from a varbinary(MAX) field (not using FileStreams) in SQL Server 2008?

When writing data to the column the VarBinary.Write() function is available in T-SQL, allowing bytes to be written to the field incrementally, but there doesn't appear to be a similar function available for reading data.

I know of the DataReader.GetBytes() method in .Net which will select just the bytes you ask for, but does this carry a performance overhead with it? i.e. will the select in sqlserver read all of the bytes in the database, and then give the getBytes() method all of these bytes for it to take the subset of bytes requested from them?

Thanks for any help.

like image 752
gmn Avatar asked Aug 17 '09 16:08

gmn


People also ask

What is the max size of Varbinary in SQL Server?

varbinary [ ( n | max) ] Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes.

How do I get the size of a Varbinary in SQL?

The VARBINARY(MAX) field allocates variable length data up to just under 2GB in size. You can use DATALENGTH() function to determine the length of the column content.

What does Varbinary mean in SQL?

VarBinary is a variable width data type. The syntax for declaring Binary variable is varbinary(n) , where n defines the maximum size in bytes. The varbinary data type uses actual length of the data entered + 2 bytes as the storage.

How can I see Varbinary data in SQL Server?

INSERT INTO #TempTable(PK, VarBinaryColumn) SELECT PK, VarBinaryColumn FROM dbo. YourPermanentTable; If you need to convert the varbinary data back to the original file text format in T-SQL, you can use CAST or CONVERT to convert to varchar or nvarchar as long as the data was originally ASCII or Unicode.


1 Answers

You use SUBSTRING. This reads a snippet from your varbinary data on the server, and only returns the snippet to the client.

[SUBSTRING] Returns part of a character, binary, text, or image expression in SQL Server.. [it] Returns binary data if expression is one of the supported binary data types. The returned string is the same type as the specified expression with the exceptions shown in the table.

like image 126
Remus Rusanu Avatar answered Sep 25 '22 15:09

Remus Rusanu