sql函数-判断某个字符串(符号分隔)是否包含另外一个字符串

Posted by Tesla9527 on November 4, 2016
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
USE [Demo.Test];
GO

/****** Object:  UserDefinedFunction [dbo].[ContainsString]    Script Date: 2016/11/4 16:57:35 ******/
SET ANSI_NULLS ON;
GO

SET QUOTED_IDENTIFIER ON;
GO


-- =============================================
-- Author:		Freem
-- Create date: 2016-10
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[ContainsString]
    (
      -- Add the parameters for the function here
      @sourceStr NVARCHAR(1000) ,
      @destStr NVARCHAR(50)
    )
RETURNS INT
AS
    BEGIN
	-- Declare the return variable here
        DECLARE @result INT = 0;
        DECLARE @singleStr NVARCHAR(100);

	-- Add the T-SQL statements to compute the return value here
        WHILE ( @result = 0
                AND CHARINDEX(',', @sourceStr, 1) > 0
              )
            BEGIN
                SET @singleStr = SUBSTRING(@sourceStr, 1,
                                           CHARINDEX(',', @sourceStr) - 1);
                IF ( @destStr = @singleStr )
                    SET @result = 1;

                SET @sourceStr = SUBSTRING(@sourceStr,
                                           CHARINDEX(',', @sourceStr, 1) + 1,
                                           LEN(@sourceStr));
            END;

        IF ( @destStr = @sourceStr )
            SET @result = 1;

	-- Return the result of the function
        RETURN @result;

    END;


GO