Quyên Nguyễn
Quyên Nguyễn

Reputation: 57

CONVERT TO VARBINARY AND BACK

I want to change eHcW080D2C0Yal1XqH/aDg== to varbinary, it become 0x654863573038304432433059616C315871482F6144673D3D.

SELECT CONVERT(varbinary, 'eHcW080D2C0Yal1XqH/aDg==')

But how can I change it back to eHcW080D2C0Yal1XqH/aDg==?

Upvotes: 0

Views: 3417

Answers (2)

Sander
Sander

Reputation: 4042

You can convert it back to a varchar type with the same convert() function.

declare @test1 varbinary(100) = CONVERT(varbinary, 'eHcW080D2C0Yal1XqH/aDg==');

select @test1;
-- output = 0x654863573038304432433059616C315871482F6144673D3D

declare @test2 varchar(100) = CONVERT(varchar(100), @test1);

select @test2;
-- output = eHcW080D2C0Yal1XqH/aDg==

Fiddle

Side note: your string looks like a password, converting it to a varbinary is not the same as encrypting it! As you can see, a simple conversion reveals the password in plain text... Passwords should be salted and hashed (background article).

Upvotes: 1

Squirrel
Squirrel

Reputation: 24813

just do a convert()

SELECT convert(varchar(50), CONVERT(varbinary, 'eHcW080D2C0Yal1XqH/aDg=='))

Upvotes: 0

Related Questions