SqlServer中varbinary转换成字符串

/*--varbinary转换成字符串

/*--测试示例

    select dbo.varbin2hexstr(0x1432fad)
--
*/

create   function  varbin2hexstr(
@bin   varbinary ( 8000 )
)
returns   varchar ( 8000 )
as
begin
    
declare   @re   varchar ( 8000 ), @i   int
    
select   @re = '' , @i = datalength ( @bin )
    
while   @i > 0
        
select   @re = substring ( ' 0123456789ABCDEF ' , substring ( @bin , @i , 1 ) / 16 + 1 , 1 )
                
+ substring ( ' 0123456789ABCDEF ' , substring ( @bin , @i , 1 ) % 16 + 1 , 1 )
                
+ @re
            ,
@i = @i - 1
    
return ( ' 0x ' + @re )
end
go

 
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页