zoukankan      html  css  js  c++  java
  • 自定义函数实现位操作

    --原帖地址:http://blog.csdn.NET/Haiwer/archive/2007/07/21/1701476.aspx

     

    --问题:

    --比如两个字符串分别是'000111011001'和'010011010011'

    --需要求他们的与,结果是000011010001

     

     

     

    /************************************************/

    /* 字符串与操作                                   */

    /* 版本:   1.0                                   */

    /* 作者: Haiwer                                  */

    /* 版权所有                                       */

    /* 调用事例:select dbo.fn_and('000111011001','010011010011')*/

    /* 2007.07.21整理                                */

    /************************************************/

    Go

    --创建函数

    CREATE function [dbo].[fn_And](

    @A1 varchar(300),

    @A2 varchar(300)

    )

    returns varchar(300)

    as

    begin

       declare @r varchar(300)

       set @r=''

       while len(@A1) >0

       begin

          set @r=@r+cast(cast(left(@A1,1) as tinyint) & cast(left(@A2,1) as tinyint) as varchar)

          set @A1=stuff(@A1,1,1,'')

          set @A2=stuff(@A2,1,1,'')

       end

       return @r

    end

     

    --测试示例

    select dbo.fn_and('000111011001','010011010011') 

    --运行结果

    /*

    000011010001

    */

     

    --问题:有表tab数据如下

    /*

    m_test'@table

    ID Val

    A 3

    A 2

    A 1

    B 4

    B 2

    C 1

    C 2

    C 8

    C 16

    B 32'

    */

    --求每个id的聚合或,要求的结果如下

    /*

    ID Val

    A 3               --3 or 2 or 1 =3

    B 38              --4 or 2 or 32=38

    C 27              -- 1 or 2 or 8 or 16=27

    */

    go

    --创建测试数据

    create table tab(ID varchar(1),Val int)

    insert into tab

    select 'A',3 union all

    select 'A',2 union all

    select 'A',1 union all

    select 'B',4 union all

    select 'B',2 union all

    select 'C',1 union all

    select 'C',2 union all

    select 'C',8 union all

    select 'C',16 union all

    select 'B',32

    --用函数实现

     

    /************************************************/

    /* 聚合或操作函数                                       */

    /* 版本:   1.0                                         */

    /* 作者: Haiwer                                       */

    /* 版权所有                                            */

    /* 调用事例:                                           */

    /* select id,[dbo].[fn_聚合或](id) as 聚合或from tab group by id */

    /* 2007.07.21整理                                      */

    /************************************************/

    go

    --创建函数

    create function [dbo].[fn_聚合或]

    (

    @id varchar(10)

    )

    returns int

    as

    begin

       declare @r int

       set @r=0

       select @r=@r | val from tab where id=@id

       return @r

    end

     

    go

    --测试示例

    select id,[dbo].[fn_聚合或](id) as 聚合或from tab group by id

    --运行结果

    /*

    id   聚合或

    ---- -----------

    A    3

    B    38

    C    27

    */

     

    --本文来自CSDN博客

    --转载请标明出处:

    --http://blog.csdn.Net/Haiwer/archive/2007/07/21/1701476.aspx

  • 相关阅读:
    [原][osg]Geometry详解
    [osg]节点遍历nodevisitor浅析
    [原][osg]osgconv浅析
    [原][游戏][攻略]仙之侠道玖章-- --从入门到放弃
    [转][linux]简单的linux下的tcp/udp
    [转][c++][跨平台]c++跨平台开发小结
    人机交互,来和我猜拳吧!
    从零开始学测试【1】测试方法术语总结
    从零开始学测试【2】网页登录界面
    JSP 和 ASP.NET 谁能主宰未来【转】
  • 原文地址:https://www.cnblogs.com/accumulater/p/6244633.html
Copyright © 2011-2022 走看看