'HASHBYTES in SQL

Why is there a 2 at the end of the SQL command on line 2? I found this set of code from a site. Can someone explain the reason for putting the 2 in there?

UPDATE myTable
SET CryptPassword = CONVERT(VARCHAR(32), HashBytes('MD5', (SELECT Password)), 2)
GO


Solution 1:[1]

The 2 sets the style ... Take a peek at https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15

Just for fun, you can compare the results

Select With1 = CONVERT(VARCHAR(32), HashBytes('MD5', 'MyPassword'), 1)  -- with 0X
      ,With2 = CONVERT(VARCHAR(32), HashBytes('MD5', 'MyPassword'), 2)  -- without 0x
      ,SansN = CONVERT(VARCHAR(32), HashBytes('MD5', 'MyPassword'))     -- binary (default)

Results

With1                               With2                               SansN
0x48503DFD58720BD5FF35C102065A52    48503DFD58720BD5FF35C102065A52D7    HP=ýXrÕÿ5ÁZR×

Note: since varchar(32), With1 is truncated (missing D7)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1