Search code examples
sqlsql-servert-sqlhashbytes

Inserting output of hashbytes() function into a table


I have a table called tbenc. Want to insert a value in the Name column which should be hash encrypted using hashbytes() T-SQL function.

create table tbenc
(
    Id int not null identity,
    Name varchar(300) null,
) 

declare @var nvarchar(200)
select @var = hashbytes('sha1', 'Nora')
print @var
insert into tbenc values(@var)

When I run:

select * from tbenc

It shows ?-s instead of the hash code.

enter image description here

Can anyone point out my mistake? Thanks in advance.


Solution

  • You need to use varbinary :

    declare @var varbinary(200)
    select @var = hashbytes('sha1', 'Nora')
    print @var;