create function f_search(@temp varchar(1000))
returns int
as
begin
       declare @flag int
       if charindex('c',@temp)>0
       begin
              set @flag=1
       end
       else
       begin
              set @flag=0
       end
       return @flag 
endselect dbo.f_search('abc')

解决方案 »

  1.   

    用charindex就行了,不需要自定义函数charindex( '子字符串', '长字符串', 1 )这样,在长字符串中查子字符串,返回0说明不在,其它说明在。
      

  2.   

    上面的兄弟,刚才我还忘了,字符串temp="a&bc&d&",s="b&c"
    则也要返回1,只要s以"&"分割成的字符有一个在temp中,则返回1,否则返回0。
      

  3.   

    上面temp="a&b&c&d&",不好意思。
      

  4.   

    直接写就行,不用函数那么麻烦declare @temp  varchar(300)set @temp='a&b&c&d&'select case when charindex('c',@temp)>0 then 1 else 0 end as flag
      

  5.   

    -- 求一用户自定义函数,功能:在字符串temp="a&b&c&d&"中查找一字符如s="c",
    -- 判断字符s是否在temp中,在则返回1,不在则返回0 
    --Test
    declare @temp varchar(50)
    declare @s varchar(10)
    declare @flag bit
    set @temp = 'a&b&c&d&'
    set @s = 'x'if charindex(@s,@temp) <> 0
    begin
    set @flag = 1
    end
    else
    begin
    set @flag = 0
    end go
    --Function
    create function Checkstr(@temp varchar(50),@s varchar(10))
    returns bit
    as
    begin
    declare @flag bit


    set @temp = 'a&b&c&d&'
    set @s = 'x'

    if charindex(@s,@temp) <> 0
    begin
    set @flag = 1
    end
    else
    begin
    set @flag = 0
    end
    end
      

  6.   

    谢谢各位的回答,刚才我还忘了,字符串temp="a&b&c&d&",s="b&c"
    则也要返回1,只要s以"&"分割成的字符有一个在temp中,则返回1,否则返回0。
      

  7.   

    创建一个拆分函数:
    CREATE FUNCTION dbo.splitstring(@str varchar(8000),@c varchar(10))
    RETURNS @t table(s varchar(100))
    AS
    BEGIN
      DECLARE @s varchar(8000)
      SET @s=@str
      WHILE CHARINDEX(@c,@s)>0
      BEGIN
        INSERT INTO @t(s) VALUES(LEFT(@s,CHARINDEX(@c,@s)-1))
        SET @s=RIGHT(@s,LEN(@s)-CHARINDEX(@c,@s))
      END
      INSERT INTO @t(s) VALUES(@s)
      RETURN
    END
    GO--调用:
    if exists(select 1 from dbo.splitstring('a&bc&d','&') a,dbo.splitstring('b&c','&') b where a.s like '%'+b.s+'%')
      print '存在'
    else
      print '不存在'
      

  8.   

    --自定义函数,判断两个数据列表字符串中,是否有任意一个数字相同if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_comp]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[f_comp]
    GO
    if exists (select * from dbo.sysobjects where id = object_id(N'[序数表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [序数表]
    GO--创建处理的序数表,主要是解决效率问题,不然可以直接在函数中生成序数表
    select top 8000 id=identity(int,1,1) into 序数表
    from sysobjects a,syscolumns b
    go--创建比较的处理函数
    create function f_comp(
    @str1 varchar(8000),
    @str2 varchar(8000)
    )returns bit
    as
    begin
    declare @re bit

    if exists(select 1 from(
    select aa='%&'+substring(@str1,id,charindex('&',@str1+'&',id)-id)+'&%'
    from 序数表 where substring('&'+@str1,id,1)='&'
    )a where '&'+@str2+'&' like aa)
    set @re=1
    else 
    set @re=0
    return(@re)
    end
    go--调用
    select dbo.f_comp('a&b&c&d','c&e')
    select dbo.f_comp('a&b&c&d','f&e')
    go