To quote the documentation:
When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.
You're specifying VARBINARY(8)
in your query, so the result is padded with zeros to that length. If you need the value without the padding for some reason, specify VARBINARY(1)
, which will return 0xA2
.
Note: They're both the same value
Alternatively, if you just want a 2 character string:
SELECT RIGHT(CONVERT(VARCHAR(8),CONVERT(VARBINARY(8),162),2), 2)
Which will return A2