做过笔记,好记性不如烂笔头:

 1 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[HEXTOINT]') and xtype in (N'FN', N'IF', N'TF'))
 2 drop function [dbo].[HEXTOINT]
 3 GO
 4 
 5 SET QUOTED_IDENTIFIER ON 
 6 GO
 7 SET ANSI_NULLS ON 
 8 GO
 9 
10 
11 
12 CREATE  function HEXTOINT(@hexstr varchar(10))
13 returns bigint
14 as
15 begin
16 if left(@hexstr,2) in ('0x','0X') set @hexstr=substring(@hexstr,3,10)
17 declare @i int, @res bigint, @l int, @c char, @ascii0 int, @asciiF int
18 select @i=1, @l=len(@hexstr), @res=0, @ascii0=ascii('0'), @asciiF=ascii('F')
19 if @hexstr is null OR @l=0 return null
20 while @i<=@l begin
21    set @c=upper(substring(@hexstr,@i,1))
22    if not ascii(@c) between @ascii0 and @asciiF return(null)
23    set @res=@res+cast(1.0 as bigint)*case when isnumeric(@c)=1 then 
24 cast(@c as int) else ascii(@c)-55 end*power(16,@l-@i) 
25    set @i=@i+1
26 end
27 return(@res)
28 end
29 
30 
31 
32 
33 GO
34 SET QUOTED_IDENTIFIER OFF 
35 GO
36 SET ANSI_NULLS ON 
37 GO

 

posted on 2014-05-08 15:11  IT高级民工  阅读(2091)  评论(0编辑  收藏  举报