字段 A: abc 
字段 B :bfd  
如果A中包含B中的任一字符, 匹配成功 

解决方案 »

  1.   


    create function f_str(@str1 varchar(20),@str2 varchar(20))
    returns bit
    as
    begin
        while len(@str1)>0
        begin
            if charindex(left(@str1,1),@str2)>0
                return 1
            
            set @str1=stuff(@str1,1,1,'')
        end
        return 0
    end
    goselect dbo.f_str('abc','bfd')
    /*
    ---- 
    1
    */select dbo.f_str('abc','dfg')
    /*
    ---- 
    0
    */
    godrop function f_str
    go
      

  2.   

    CREATE FUNCTION dbo.f_stra(@a varchar(50),@b varchar(50))
    RETURNS varchar(8000)
    AS
    BEGIN
    declare @n int
    set @n =1while @n <= len(@b) 
    begin

      if charindex( substring(@a,@n-1,@n), @a) > 1
    return '匹配' set @n =@n + 1
    end
     
    return '不匹配'
    END
    godeclare @tb table
    (
    a varchar(50),
    b varchar(50)
    )insert into @tb values('abc','bfd')
    insert into @tb values('abc','fd')select *,dbo.f_stra(a,b)
    from @tb结果:
    abc bfd 匹配
    abc fd 不匹配
      

  3.   

    declare @s_no varchar(10),@c_no varchar(10),@yn int
    declare @num int
    set @yn=0
    set @num=1
    set @s_no='abc'
    set @c_no='bfd'
    while @num<=len(@s_no)
    begin
    set @yn=@yn+(select (case when (select charindex(substring(@s_no,@num,1),@c_no))=0  then 0  else 1 end))
    set @num=@num+1
    endselect case when @yn=1 then '匹配成功' else '匹配不成功' end/**
    匹配成功