--自己写用户定义函数
create function f_Char2Binary(@str varchar(8000))
returns varchar(8000)
AS
BEGIN
DECLARE @r varbinary(8000),@i int,@j int
select @r=cast(@str as varbinary(8000)),@i=datalength(@r),@str=''
while @i>0
begin
select @j=substring(@r,@i,1),
@str=substring('0123456789ABCDEF',@j/16+1,1)
+substring('0123456789ABCDEF',@j%16+1,1)
+@str,
@i=@i-1
end
return(@str)
end
go
--调用
select dbo.f_Char2Binary('我希望的到16进制的字符串,如何可以实现')
go
drop function f_Char2Binary
/*--结果
------------------------------------
CED2CFA3CDFBB5C4B5BD3136BDF8D6C6B5C4D7D6B7FBB4AEA3ACC8E7BACEBFC9D2D4CAB5CFD6
(所影响的行数为 1 行)
--*/