एमएस-एसक्यूएल 2008 के लिए निम्नलिखित संग्रहित प्रो एक हेक्स स्ट्रिंग को वर्चर (अधिकतम) में परिवर्तित कर देगा:
if exists (select * from dbo.sysobjects where name = 'f_hextostr' and xtype = 'FN')
drop function [dbo].[f_hextostr]
GO
CREATE FUNCTION [dbo].[f_hextostr] (@hexstring VARCHAR(max))
RETURNS VARCHAR(max)
AS
begin
declare @char1 char(1), @char2 char(1), @strlen int, @currpos int, @result varchar(max)
set @strlen=len(@hexstring)
set @currpos=1
set @result=''
while @currpos<@strlen
begin
set @char1=substring(@hexstring,@currpos,1)
set @char2=substring(@hexstring,@currpos+1,1)
if (@char1 between '0' and '9' or @char1 between 'A' and 'F')
and (@char2 between '0' and '9' or @char2 between 'A' and 'F')
set @[email protected]+
char((ascii(@char1)-case when @char1 between '0' and '9' then 48 else 55 end)*16+
ascii(@char2)-case when @char2 between '0' and '9' then 48 else 55 end)
set @currpos = @currpos+2
end
return @result
end
GO
उपयोग करने के लिए बस कुछ ऐसा करें:
select dbo.f_hextostr('0x3031323')
या
select dbo.f_hextostr(X) from MyTable