question

Pooja avatar image
Pooja asked

Convert Varbinary to Varchar

Hi,

I have a column in my table having datatype as Varbinary(20). The values in the column are populated by combining the values from other columns using SHA algorithm. Now I want to retrieve the original value in varchar datatype. How to do that. Thanks in advance

sql-server-2008cast-converthash
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image
Matt Whitfield answered

You can't reverse an SHA hash, it's a one-way algorithm...

To convert a normal varbinary to a varchar, you use CONVERT.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image
Grant Fritchey answered

I agree with Matt but I'd use CAST instead of CONVERT

7 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
In general I'd agree, but I find going from varbinary to varchar without specifying style using CONVERT is usually not what is needed...
1 Like 1 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
Mainly, because MS keeps pushing CAST over CONVERT and making threats to take it away. So I try to force myself to use it everywhere I can. It's not always the best choice, but I try to build coding habits so...
1 Like 1 ·
Katie 1 avatar image Katie 1 commented ·
@ Grant,@Matt, @fatherjack, is there a way to insert a vachar values into the varbinary column?
1 Like 1 ·
TimothyAWiseman avatar image TimothyAWiseman commented ·
Remember that CAST is part of ANSI sql and convert is part of Microsoft T-SQL. Many people, especially if they use multiple RDBMS, consider it a best practice to use ANSI SQL whenever possible and only use proprietary extensions when there is a good reason to.
1 Like 1 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
@Grant - why the preference for CAST? I usually go for CAST and then if style is needed swap to CONVERT but for no reason other than habit ...
0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.