如何比较两个逗号分隔的字符串,并返回false直到和除非所有值匹配

时间:2014-11-12 07:39:23

标签: sql sql-server

suppose @v1 = p1;p2;p3;p4
@v2 = p1;p2;p3
@v3= p1;p2;p3;p4

我必须将@ v2和@ v3的值与@ v1的值进行比较。 如果所有值都匹配,我需要一个返回TRUE的函数。所以在上面的例子中,它在@ v3的情况下返回TRUE,在@ v2的情况下返回false。

我正在尝试编写代码..但它不起作用。 我在这里提到代码。

SELECT CASE WHEN EXISTS 
(
  SELECT 1 
    FROM dbo.Split(@v1) AS a
    INNER JOIN dbo.Split(@v2) AS b
    ON a.Item = b.Item
)
THEN 1 ELSE 0 END;


how can it be possible in sql server? plz help.

3 个答案:

答案 0 :(得分:0)

对于所有值匹配,您可以使用相同的运算符:

DECLARE @v1 VARCHAR(20) = 'p1;p2;p3;p4'
DECLARE @v2 VARCHAR(20) = 'p1;p2;p3'
DECLARE @v3 VARCHAR(20) = 'p1;p2;p3;p4'

IF @v1 = @v2
  PRINT 'V1=V2'
ELSE IF @v1 = @v3
  PRINT 'V1=V3' 

IF EXISTS(SELECT 1 
    FROM dbo.Split(@v1) AS a
    INNER JOIN dbo.Split(@v2) AS b
    ON a.Item = b.Item)
SELECT 1
ELSE
SELECT 0

假设你的函数是表值。

答案 1 :(得分:0)

我在测试后修改了我的答案:

CREATE FUNCTION CommaListsAreEqual
(
    @CommaList1 varchar(1000),
    @CommaList2 varchar(1000)
)
RETURNS BIT
AS
BEGIN
  DECLARE @Retval INT;
  WITH 
    Table1 (Item) 
    AS 
    (
      SELECT Item FROM dbo.Split(@CommaList1, ',')
    ),
    Table2 (Item)  
    AS 
    (
      SELECT Item FROM dbo.Split(@CommaList2, ',')
    )
     SELECT
       @Retval = COUNT(*)
     FROM
       Table1
       FULL OUTER JOIN Table2 ON Table1.Item = Table2.Item
     WHERE
       Table1.Item IS NULL 
       OR
       Table2.Item IS NULL


   RETURN CASE WHEN @Retval = 0 THEN 1 ELSE 0 END

END
GO

-- Tests:
SELECT dbo.CommaListsAreEqual('1,2,3', '1,2,3') -- Should return 1
SELECT dbo.CommaListsAreEqual('1,2,3', '1,2,5') -- Should return 0

答案 2 :(得分:0)

试试这个。

CREATE FUNCTION Checks(@v1 VARCHAR(100),@v2 VARCHAR(100),@v3 VARCHAR(100))
RETURNS INT
AS
  BEGIN
      RETURN
        (SELECT Count(1)
         FROM   (SELECT Split.a.value('.', 'VARCHAR(100)') col
                 FROM   (SELECT Cast ('<M>' + Replace(@v1, ';', '</M><M>') + '</M>' AS XML) AS Data) AS f
                        CROSS APPLY Data.nodes ('/M') AS Split(a)) fst
         WHERE  NOT EXISTS (SELECT *
                            FROM   (SELECT Split.a.value('.', 'VARCHAR(100)') col
                                    FROM   (SELECT Cast ('<M>' + Replace(@v2, ';', '</M><M>') + '</M>' AS XML) AS Data) AS A
                                           CROSS APPLY Data.nodes ('/M') AS Split(a)
                                    UNION
                                    SELECT Split.a.value('.', 'VARCHAR(100)') col
                                    FROM   (SELECT Cast ('<M>' + Replace(@v3, ';', '</M><M>') + '</M>' AS XML) AS Data) AS A
                                           CROSS APPLY Data.nodes ('/M') AS Split(a)) kl
                            WHERE  fst.col = kl.col))
  END

DECLARE @v1 VARCHAR(100) = 'p1;p2;p3;p4',
        @v2 VARCHAR(100) = 'p1;p2;p3;p4',
        @v3 VARCHAR(100) = 'p1;p2;p3'

SELECT CASE WHEN dbo.Checks(@v1, @v2, @v3) = 0 THEN 'MATCHED' ELSE 'NOT MATCHED' END [Check]
-- OUTPUT : 'MATCHED'

DECLARE @v1 VARCHAR(100) = 'p1;p2;p3;p4',
        @v2 VARCHAR(100) = 'p1;p2;p3',
        @v3 VARCHAR(100) = 'p1;p2;p3'

SELECT CASE WHEN dbo.Checks(@v1, @v2, @v3) = 0 THEN 'MATCHED' ELSE 'NOT MATCHED' END [Check]
-- OUTPUT : 'NOT MATCHED'