Hash Code in SQL Server?

2019-07-20 03:35发布

问题:

I am trying to solve a unique problem. Thanks for any help with this.

Is there an easy way to generate some kind of a hash code for a record based on several of its field values?

I thought about just creating a view with an extra column that concatenates all of the values that I want to check, but I'm checking to see if there is a better way.

I am trying to identify records based on their values, because I need to group parent records by the uniqueness of these child records, if that makes any sense.

Thanks again for any advice.

回答1:

SELECT
    HASHBYTES('MD5', CAST(col1 AS VARBINARY(MAX)) + 
        CAST(col2 AS VARBINARY(MAX)))
FROM
    mytable