Share via


How to insert a Hash value in a table

Question

Monday, November 21, 2016 6:38 PM

Hi,

I want to insert a hash value in a table the query exectuted successfuly but the value in the table is not correct

insert into testDS
SELECT HASHBYTES('SHA', 'Steve Jones') 

Value of the hash :

SELECT HASHBYTES('SHA', 'Steve Jones') ==>0x2E978DE4841B1F3651A8DF4B2D2CF5F5C624A76B

Value in table 

select * from testDS  ===> .—ä„6Q¨ßK-,õõÆ$§k

All replies (3)

Monday, November 21, 2016 6:43 PM ✅Answered | 2 votes

What is the data type of testDS? It should be varbinary(8000)  or nvarbinary(4000) where it is stored as a binary value.

Don't expect to be able to read it.

What did you expect to see?


Monday, November 21, 2016 6:57 PM ✅Answered | 1 vote

CREATE TABLE [dbo].[Table_Hash](
[OrigName] [varchar](50) NOT NULL,
[HashName] [varbinary](max) NOT NULL

GO

Insert Table_Hash
Select 'Kevin', hashBytes('SHA','Kevin')
Insert Table_Hash
Select 'Steve Jones', hashBytes('SHA','Steve Jones')

Select * from Table_Hash

OrigName HashName
Kevin 0xE043899DAA0C7ADD37BC99792B2C045D6ABBC6DC
Steve Jones 0x2E978DE4841B1F3651A8DF4B2D2CF5F5C624A76B

22 years of database experience, most with SQL Server. Please 'Mark as answered' those posts that helped you.

Kevin3NF on Twitter
DallasDBAs


Monday, November 21, 2016 8:24 PM ✅Answered

I think you used varchar as the data type, you should use varbinary (nvarchar may not work either).

--DECLARE @hashbytes varchar(8000)
--DECLARE @hashbytes nvarchar(4000)
DECLARE @hashbytes varbinary(8000)
SELECT @hashbytes = HASHBYTES('SHA', 'Steve Jones') 
SELECT @hashbytes

A Fan of SSIS, SSRS and SSAS