5

I would like to create unique string columns (32 characters in length) from combination of columns with different data types in SQL Server 2005.

2 Answers 2

8

I have found out the solution elsewhere in StackOverflow

SELECT SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', 'HelloWorld')), 3, 32)

The answer thread is here

Sign up to request clarification or add additional context in comments.

1 Comment

Please note that beginning with SQL Server 2016, all algorithms other than SHA2_256 and SHA2_512 are deprecated.Older algorithms (not recommended) will continue working, but they will raise a deprecation event. learn.microsoft.com/en-us/sql/t-sql/functions/…
2

With HASBYTES you can create SHA1 hashes, that have 20 bytes, and you can create MD5 hashes, 16 bytes. There are various combination algorithms that can produce arbitrary length material by repeated hash operations, like the PRF of TLS (see RFC 2246).

This should be enough to get you started. You need to define what '32 characters' mean, since hash functions produce bytes not characters. Also, you need to internalize that no algorithm can possibly produce hashes of fixed length w/o collisions (guaranteed 'unique'). Although at 32 bytes length (assuming that by 'characters' you mean bytes) the theoretical collision probability of 50% is at 4x1038 hashed elements (see birthday problem), that assumes a perfect distribution for your 32 bytes output hash function, which you're not going to achieve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.