Sql查询查找相关匹配

时间:2016-03-01 10:45:55

标签: sql database

我正在尝试编写一个应该返回以下输出的查询:

val1 ='伟大' val2 ='TAGER'

见上面有相同的字符,如果找到匹配则应返回'Exist'。否则,如果缺少某个字符,则“不存在”。我正在使用CASE来获取所需的输出,并想知道是否可以使用SUBSTRING,CHARINDEX等来完成。希望得到一个解决方案或想法。感谢。

2 个答案:

答案 0 :(得分:0)

您可以根据需要将其作为功能/程序。

DECLARE @S1 VARCHAR(100)
DECLARE @S2 VARCHAR(100)

SELECT @S1 = val1
    ,@S2 = val2
FROM <TABLE_NAME>

DECLARE @c CHAR(1)
DECLARE @i TINYINT
DECLARE @o1 VARCHAR(100) = ''
DECLARE @o2 VARCHAR(100) = ''

WHILE DataLength(@s1) > 0
BEGIN
    SET @c = Left(@s1, 1)
    SET @s1 = Substring(@s1, 2, len(@s1))
    SET @i = 1

    WHILE @i <= Len(@o1)
        AND @c > substring(@o1, @i, 1)
        SET @i += 1
    SET @o1 = left(@o1, @i - 1) + @c + substring(@o1, @i, len(@o1))
END

WHILE DataLength(@s2) > 0
BEGIN
    SET @c = Left(@s2, 1)
    SET @s2 = Substring(@s2, 2, len(@s2))
    SET @i = 1

    WHILE @i <= Len(@o2)
        AND @c > substring(@o2, @i, 1)
        SET @i += 1
    SET @o2 = left(@o2, @i - 1) + @c + substring(@o2, @i, len(@o2))
END

SELECT CASE 
        WHEN @o1 = @o2
            THEN 'Exist'
        ELSE 'Not Exist'
        END

答案 1 :(得分:0)

这是一个自定义脚本 首先运行此SP

    IF(OBJECT_ID('CharSplit')) IS NOT NULL
    DROP PROCEDURE CharSplit;
GO
CREATE PROC CharSplit
       @Words VARCHAR(MAX)
AS
     BEGIN
         IF OBJECT_ID('tempdb..#temp1') IS NOT NULL
             DROP TABLE #temp1;
         CREATE TABLE #temp1
                            (
                      letter CHAR(1), freq INT
                            );
         IF OBJECT_ID('tempdb..#temp2') IS NOT NULL
             DROP TABLE #temp2;
         CREATE TABLE #temp2
                            (
                      letter CHAR(1), freq INT
                            );
         DECLARE @t VARCHAR(MAX);
         DECLARE @I INT;
         --SET @Words = 'sanuantony';
         SELECT @I = 0;
         WHILE(@I < LEN(@Words) + 1)
             BEGIN
                 SELECT @t = SUBSTRING(@words, @I, 1);
                 INSERT INTO #temp1
                                   (letter, freq
                                   )
                 VALUES
                        (@t, 0
                        );
                 SET @I = @I + 1;
             END;         
         TRUNCATE TABLE #temp2;
         INSERT INTO #temp2
                           (letter, freq
                           )
                SELECT letter, COUNT(freq)
                FROM #temp1
                GROUP BY letter;
         SELECT *
         FROM #temp2
         ORDER BY letter;
     END;

现在试试你的业务逻辑

 DECLARE @t1 AS TABLE
                     (
                      letter CHAR(1), freq INT
                     );
 DECLARE @t2 AS TABLE
                     (
                      letter CHAR(1), freq INT
                     );
 INSERT INTO @t1
 EXEC charSplit 'alammalay';

 INSERT INTO @t2
 EXEC charSplit 'malayalam';

 IF(
    (
     SELECT COUNT(1)
     FROM @t1
    ) =
        (
         SELECT COUNT(1)
         FROM @t2
        )
    AND
        (
         SELECT COUNT(1)
         FROM @t2
        ) =
            (
            (
             SELECT COUNT(1)
             FROM @t1
            )
            UNION
            (
             SELECT COUNT(1)
             FROM @t2
            )
            )
   )
     SELECT 'Both are matching' AS result;
 ELSE
 SELECT 'Both are not matching' AS result;