Why do I use the following command in SQL Server 2005
select convert(varbinary(16),N'123')
returns 0x310032003300
and not 1111011
what would be the binary value?
Why do I use the following command in SQL Server 2005
select convert(varbinary(16),N'123')
returns 0x310032003300
and not 1111011
what would be the binary value?
@dellasavia, 1111011
is only 1 byte, in the case a N'123 'is a nvarchar with 3 characters.
Since each nvarchar character occupies 2 bytes, totaling 6 bytes.
0x310032003300
can be read as follows in HEX:
HEX: 31
00
32
00
33
00
The Binary in this case would look something like:
Binary: 00110001
00000000
00110010
00000000
00110011
00000000
But if instead of using an nvarchar, we did the conversion of a tinyint, then you would get the expected result.
DECLARE @valor as tinyint
SET @valor = 123
SELECT convert(binary(1), @valor)
But in this case, you would see an output 0x7B
(HEX), which is the same as 01111011
(Binary).