VARBINARY is a binary string data type that holds variable-length sequences of bytes. Unlike BINARY, which right-pads values to a fixed length, VARBINARY only stores the actual number of bytes inserted, up to the declared maximum. It is ideal for data that is not human-readable, including images, documents, encryption hashes, and compressed objects.The maximum length depends on the database:- SQL Server: up to 8,000 bytes for VARBINARY(n) and 2 GB for VARBINARY(MAX).- MySQL: up to 65,535 bytes (subject to row size limits).Storage is byte-oriented, so character set and collation rules do not apply. Most functions that work on strings (e.g., LIKE) are not meaningful on VARBINARY. Use hexadecimal or parameterized input when inserting literal values.Caveats:1. Implicit conversions from strings may change data if the string uses a character set with multi-byte characters.2. Sorting and comparison are byte-by-byte, not lexicographic text order.3. Large values can impact memory and network bandwidth. Prefer streaming APIs (e.g., FILESTREAM, BLOB streaming) for very large blobs.4. In replication or backup scenarios, large VARBINARY columns can slow operations.
length
(integer) - Maximum number of bytes to store. Use MAX in SQL Server for up to 2 GB.SQL Server 7.0 and MySQL 4.1
BINARY pads values to the fixed length specified, which can waste space. VARBINARY stores only the bytes you insert, so it is more space-efficient for variable-length data.
Yes. Both MySQL and SQL Server allow indexing VARBINARY, but large keys slow inserts and lookups. Consider hashing long binary values before indexing.
SQL Server offers up to 8,000 bytes for VARBINARY(n) and 2 GB with VARBINARY(MAX). MySQL supports up to 65,535 bytes, limited by row size.
Use CAST or CONVERT with an appropriate character type and code page, for example CAST(binary_col AS VARCHAR(100)) in SQL Server. Make sure the bytes represent valid text in that encoding.