Generate MD5 hash string with T-SQL

后端 未结 9 1948
自闭症患者
自闭症患者 2020-12-02 08:52

Is there a way to generate MD5 Hash string of type varchar(32) without using fn_varbintohexstr

SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes(\'MD5\', \'em         


        
9条回答
  •  孤城傲影
    2020-12-02 09:32

    You didn't explicitly say you wanted the string to be hex; if you are open to the more space efficient base 64 string encoding, and you are using SQL Server 2016 or later, here's an alternative:

    select SubString(h, 1, 32) from OpenJson(
        (select HashBytes('MD5', 'email@dot.com') h for json path)
    ) with (h nvarchar(max));
    

    This produces:

    9TvQiSDl0lgJ3yVj75xStg==
    

提交回复
热议问题