且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 SQL Server 上将 varbinary 转换为字符串

更新时间:2022-12-12 08:48:37

varbinary 转换为 varchar"可能意味着不同的事情.

"Converting a varbinary to a varchar" can mean different things.

如果 varbinary 是 SQL Server 中字符串的二进制表示(例如通过直接转换为 varbinary 或从 DecryptByPassPhraseDECOMPRESS 函数)你可以 CAST

If the varbinary is the binary representation of a string in SQL Server (for example returned by casting to varbinary directly or from the DecryptByPassPhrase or DECOMPRESS functions) you can just CAST it

declare @b varbinary(max)
set @b = 0x5468697320697320612074657374

select cast(@b as varchar(max)) /*Returns "This is a test"*/

这等效于使用带有 0 样式参数的 CONVERT.

This is the equivalent of using CONVERT with a style parameter of 0.

CONVERT(varchar(max), @b, 0)

其他样式参数可与 CONVERT 一起使用,以满足其他答案中所述的不同要求.

Other style parameters are available with CONVERT for different requirements as noted in other answers.