Siam Parvez
Siam Parvez

Reputation: 123

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

Upvotes: 1

Views: 1178

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81930

The 2 sets the style ... Take a peek at https://learn.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)

Upvotes: 4

Related Questions