Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)


By Clinton Herring,

A function to convert varbinary to hex string. Have you every tried to deal with varbinary via T-SQL? Passwords and SIDs are of this datatype. In Query Analyzer this datatype displays as hex string but under the hood it is binary. Try concatenating a varbinary to a string in a print statement and you get a datatype mismatch error. Try to convert this datatype to varchar and you get garbage. You can use the little know extended stored procedure xp_varbintohexstr. It takes one varbinary input parameter and outputs the equivalent hex string as an OUTPUT parameter but it is not well suited for set processing. Ufn_vbintohexstr is. You can use it directly in a select statement against a column if necessary. Remember to include the parentheses when using this function in a select statement but don't include them when calling it via Exec. The input is a varbinary(256) so any length variable up to this max can be converted. The function returns the equivalent hex string of the varbinary input value. BTW a hex string doesn't convert back into a varbinary via the convert function so take care using the results and don't forget to include 2 extra position for the leading '0x' in your output variables. -- Fast & efficient update to original.

Total article views: 218 | Views in the last 30 days: 1
Related Articles

Getting the VARBINARY data into a VARCHAR field



convert the datatype string to date

convert the datatype string to date


Convert String DataType to Date datatype in DTS

Convert String DataType to Date datatype in DTS


convert datatype DT_STRING to DT_DBDATE in ssis

convert datatype DT_STRING to DT_DBDATE in ssis


Huge varbinary or image to hex string

Convert huge varbinary or image data to a hexadecimal string


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones