sql >> Database >  >> RDS >> Sqlserver

vergelijk door komma's gescheiden waarden in sql

Is dit wat u zoekt?

Waar/onwaar resultaten

-- matches only those values which exist in both CSV sets
SELECT T1.[Item], CASE  WHEN T2.[Item] IS NULL THEN 0 ELSE 1 END AS [Match] 
FROM [dbo].[Split]('val1,val2,val3', ',') AS T1
    LEFT JOIN [dbo].[Split]('val3,val4', ',') AS T2 on T1.[Item] = T2.[Item]

Retourneren

Item    Match
val1    0
val2    0
val3    1

Alleen echte overeenkomsten

-- matches only those values which exist in both CSV sets
SELECT T1.[Item] 
FROM [dbo].[Split]('val1,val2,val3', ',') AS T1
    INNER JOIN [dbo].[Split]('val3,val4', ',') AS T2 on T1.[Item] = T2.[Item]

Retourneren

Item
val3

Splitsfunctie

CREATE FUNCTION [dbo].[Split] 
(   
    @s VARCHAR(max),
    @split CHAR(1)
)
RETURNS @temptable TABLE ([Item] VARCHAR(MAX))    
AS
BEGIN
    DECLARE @x XML

    SELECT @x = CONVERT(xml,'<root><s>' + REPLACE(@s,@split,'</s><s>') + '</s></root>');

    INSERT INTO @temptable          
    SELECT [Value] = T.c.value('.','varchar(20)')
    FROM @X.nodes('/root/s') T(c);
RETURN
END;


  1. MySQLDataReader haalt Null-waardeprobleem op in c#

  2. Installeer mysql-module voor python met pip

  3. Zoekopdracht op coördinaten duurt te lang - opties om te optimaliseren?

  4. SQL Server-systeemdatabases - MSDB-onderhoud