• sql 自定义函数-16进制转10进制


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

     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
  • 相关阅读:
    生活网站
    input 输入值的监听 禁止输入特殊字符
    jq 插件分享
    css3 特效分享
    sharepoint---RBS回收站清空设置
    &&和||
    DataTable得到某行某列的值
    后台刷新当前页面和弹出对话框跳转页面
    c#.netGr idView1在div不局中
    DIV UL LI
  • 原文地址:https://www.cnblogs.com/linximf/p/3716232.html
Copyright © 2020-2023  润新知