课程表
课程      周次
计算机    11111111111000000000(周次意思为1-11周)
软件工程  00111111100000000000(周次意思为5-9周)
现在判断两门课的周次是否有交叉点,这个怎么做啊?
想寻找一个按位比较的函数,只要周次的同一位数都为1则交叉,不知道有没有类试函数哟?

解决方案 »

  1.   

    有位运算符可以做。但数据类型必须是INT,SMALLINT,TINYINT.
    先将11111111111000000000,00111111100000000000转换为十进制a,b
    select a$b  
      

  2.   

    if object_id('t_and') is not null
       drop table t_and
    if object_id('dbo.f_getcrossaaa') is not null
       drop function dbo.f_getcrossaaa
    go
    create table t_and(课程 varchar(100),周次 varchar(30))
    insert t_and 
    select '计算机','11111111111000000000' union all
    select '软件工程','00111111100000000000'
    go
    create function dbo.f_getcrossaaa(@s1 varchar(30),@s2 varchar(30))
    returns varchar(10)
    as
    begin
       declare @begin int,@end int,@result varchar(10)
       select @begin=1,@end=len(@s1),@result=''
       while @begin<=@end
       begin
          declare @1 int,@2 int
          select @1=cast(substring(@s1,@begin,1) as int),@2=cast(substring(@s2,@begin,1) as int) 
          if (@1&@2=1)
               set @result='有'
               set @begin=@end;
          else if(@1&@2!=1)
               select @begin=@begin+1
       end 
       return @result
    end
    谁能帮看看,错了,找不出来,愁人啊
    关键字 'else' 附近有语法错误。
      

  3.   

    if ....
    beginend elsebeginend
      

  4.   


    ...
                declare   @1   int,@2   int
                select   @1=cast(substring(@s1,@begin,1)   as   int),@2=cast(substring(@s2,@begin,1)   as   int)  
                if   (@1&@2=1) 
                          select   @result='有', @begin=@end
                else   if(@1&@2!=1)
                          select   @begin=@begin+1
    ...or...
                declare   @1   int,@2   int
                select   @1=cast(substring(@s1,@begin,1)   as   int),@2=cast(substring(@s2,@begin,1)   as   int)  
                if   (@1&@2=1) begin
                          set @result='有'
                          set @begin=@end
                          end
                else   if(@1&@2!=1)
                          select   @begin=@begin+1
    ...
      

  5.   

    create function getTa(@b varchar(200))
    returns @a table(id int identity(1,1),a char(1))
    as
    begin
    declare @i int
    set @i=1
    while @i<=len(@b)
    begin
    insert @a select substring(@b,@i,1)
    set @i=@i+1
    end
    return
    enddeclare @a table(a varchar(20),b varchar(100))
    insert @a select '计算机'  ,'11111111111000000000'
    union all select '软件工程','00111111100000000000' 
    union all select '高数'    ,'00101110110000000000' declare @c varchar(100),@d varchar(100)
    select @c=b from @a where a='计算机'
    select @d=b from @a where a='软件工程'
    select  a.id from 
    dbo.getTa(@c) a 
    inner join 
    dbo.getta(@d) b 
    on a.id=b.id and a.a=b.a where a.a=1--result
    /*id          
    ----------- 
    3
    4
    5
    6
    7
    8
    9(所影响的行数为 7 行)*/
      

  6.   

    太经典了,谢谢!!  JL99000  tim_spac   chuifengde  benbenkui    wzy_love_sly,各位前辈为我解决了一个大问题