VARBINARY (TSQL Datatype)
Is a variable-length binary datatype, with precision being the maximum length in bytes.
Syntax
VARBINARY [( precision )]
Range
The range of precision is 1 to 8000.
- When precision is not specified in declarations, the default is 1.
- When precision is not specified inside a
CAST
function, the default is 30.
The storage size of a varbinary
data is not affected by the value of precision. Only the actual size of the data consumes memory or disk space.
Literal Binary String
A literal binary string begins with the prefix 0x
or 0X
, followed by pairs of hexadecimal digits:
0xab
0x1234
0xAB1234
0x -- empty binary string
Examples
DECLARE @a VARBINARY(100) = 0xEEFF;
DECLARE @b VARBINARY(100);
DECLARE @c VARBINARY; -- default precision is 1
SET @b = 0x1234abcd;
SET @c = 0x78
PRINT @a + @b + @c;
SET @c = 0x56789a; -- the assignment SILENTLY TRUNCATES the binary literal, as precision of @c is 1
PRINT @c;
-- if precision is omitted in a CAST, the default precision is 30
PRINT CAST(0x112233445566778899001122334455667788990011223344556677889900abcdef AS VARBINARY);
The result is:
0xeeff1234abcd78
0x56
0x112233445566778899001122334455667788990011223344556677889900 <--- truncated to 30 bytes