博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL位移运算函数
阅读量:6900 次
发布时间:2019-06-27

本文共 1129 字,大约阅读时间需要 3 分钟。

-- =============================================

-- Author:      <maco_wang>

-- Create date: <2011-03-22>

-- Description: <SQL位移运算函数>

-- =============================================

--创建函数

create function displacement(@n as bigint,@m as varchar(3))

returns int

as

begin

    declare @maco varchar(50);set @maco=''

    declare @i int

    declare @x int

    declare @s int

    while (@n<>0)

    begin

       set @maco=@maco+convert(char(1),@n%2)

       set @n=@n/2

    end

    set @maco=reverse(@maco)

    set @maco=RIGHT('0000'+@maco,4)

    set @s=LEN(@maco)

    set @i=convert(int,RIGHT(@m,1))

    set @x=1

    if LEFT(@m,2)='<<'

    begin

      while(@x<=@i)

      begin

       select @maco=@maco+'0'

       select @x=@x+1

      end

      select @maco=RIGHT(@maco,@s)

    end

    if LEFT(@m,2)='>>'

    begin

      while(@x<=@i)

      begin

       select @maco='0'+@maco

       select @x=@x+1

      end

      select @maco=LEFT(@maco,@s)

    end

    set @i=LEN(@maco)

    set @s=0

    while(@i>0)

    begin

      if SUBSTRING(@maco,LEN(@maco)-@i+1,1)='1'

      begin

       select @s=@s+POWER(2,convert(float,@i-1))

      end

      select @i=@i-1

    end

    return @s

end

 

--示例

select dbo.displacement(1,'<<3') 

select dbo.displacement(8,'>>1')

select dbo.displacement(12,'>>3')

 

--运行结果

/*

8

4

1

*/

转载于:https://www.cnblogs.com/accumulater/p/6244687.html

你可能感兴趣的文章
察看FreeBSD日志信息
查看>>
EMOS1.5更新
查看>>
mantis上传文件设置与存放路径
查看>>
Lync for iphone
查看>>
SQL SERVER数据库权限
查看>>
Nginx 基础篇(二)
查看>>
Linux 中 10 个有用的命令行补全例子
查看>>
【思想篇之爱左看右】
查看>>
Hadoop2.6+Zookeeper3.4+Hbase1.0部署安装
查看>>
测试唯一ID支持多大的并发量
查看>>
centos 安装部署docker与局域网主机相通详细配置
查看>>
老鸟经验谈linux运维人员到底要不要考linux认证
查看>>
solr配置
查看>>
CSS HACK 区别于ie6/7/8/firefox的小问题
查看>>
编译一个可以用Qemu进行Debug的Linux Kernel:
查看>>
linux 服务器 keras 深度学习环境搭建
查看>>
xshell+xmanager远程linux图形化界面
查看>>
布局模型
查看>>
我的友情链接
查看>>
jquery 实现复选框 全选/反选
查看>>