• 主页
• 基础类
• 应用实例
• 新技术前沿

# 想寻找一个按位比较的函数？请前辈指点

salecn 2007-12-19 12:10:05

...全文
84 点赞 收藏 8

8 条回复

salecn 2007-12-24

chuifengde 2007-12-19
``````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
end

declare @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 行）

*/``````

wzy_love_sly 2007-12-19

tim_spac 2007-12-19
``````
...
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
...
``````

wzy_love_sly 2007-12-19
if ....
begin

end

else

begin

end

JL99000 2007-12-19
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

benbenkui 2007-12-19

benbenkui 2007-12-19

select a\$b

MS-SQL Server

3.2w+

MS-SQL Server相关内容讨论专区