Binary Styles Supported by CONVERT() in SQL Server

Below is a list of binary styles supported by the CONVERT() function in SQL Server. These are the styles that can be used as the third argument to indicate how the input value is formatted when converting to binary.

We can use these styles when the input expression is binary(n), char(n), varbinary(n), or varchar(n).

ValueOutput
0 (default)Translates ASCII characters to binary bytes, or binary bytes to ASCII characters. Each character or byte is converted 1:1.

When converting to a binary data type, the characters 0x are added to the left of the result.
12When converting to a binary data type, the input expression must be a character expression. The input expression must have an even number of hexadecimal digits.

If the style argument is set to 1, the expression must have 0x as the first two characters. If the input expression contains an odd number of characters, or if any of the characters is invalid, an error is raised.

If the length of the converted expression exceeds the length of the data type, the result is right truncated.

Fixed length data types larger than the converted result has zeros added to the right of the result.

Character types require a binary expression. Each binary character is converted into two hexadecimal characters. If the length of the converted expression exceeds the length of the data type, it’s truncated.

For a fixed size character type, if the length of the converted result is less than its length of the data type, spaces are added to the right of the converted expression to maintain an even number of hexadecimal digits.

For style 2, the characters 0x aren’t added to the left of the converted result.