Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

bandingkan nilai yang dipisahkan koma di sql

Apakah ini yang Anda cari?

Hasil Benar / Salah

-- 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]

Pengembalian

Item    Match
val1    0
val2    0
val3    1

Hanya yang benar-benar cocok

-- 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]

Pengembalian

Item
val3

Fungsi pemisahan

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Melewati parameter Tabel Bernilai ke prosedur tersimpan

  2. Bagaimana cara mendapatkan daftar Server SQL yang tersedia menggunakan Kode C #?

  3. Database Sistem SQL Server – Pemeliharaan MSDB

  4. sp_generate_inserts untuk SQL Server 2008

  5. Pemicu SQL Server - urutan eksekusi