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

Memisahkan nilai yang dibatasi dalam kolom SQL menjadi beberapa baris yang menggunakan beberapa pembatas

Coba ini:

Berdasarkan artikel dari di sini

;with T(id, Cols) as
(
  select 1, '10 x 1234 description1,11 x 456 description2' 
  UNION ALL
  select 2, '40 x 123478 description3,51 x 45678 description4' 
),
--first split all comma separated into different rows
CTE2 AS 
(
    SELECT id, 
    CAST(N'<H><r>' + replace(Replace(Vals.a.value('.', 'NVARCHAR(50)'),
        ' ','|'), '|', '</r><r>') + '</r></H>' as XML) Cols
    FROM
    (
    SELECT *,CAST (N'<H><r>' + Replace(cols,',','</r><r>') + 
        '</r></H>' AS XML) AS [vals]
    FROM T) d
    CROSS APPLY d.[vals].nodes('/H/r') Vals(a)
)
-- split all ' ' demilited values now
SELECT distinct id, Vals.a.value('(/H/r)[1]', 'VARCHAR(100)') AS Qty,
Vals.a.value('(/H/r)[2]', 'VARCHAR(100)') AS X,
Vals.a.value('(/H/r)[3]', 'VARCHAR(100)') AS ItemNo,
Vals.a.value('(/H/r)[4]', 'VARCHAR(100)') AS Description
FROM
(
SELECT *
FROM CTE2) d
CROSS APPLY d.[cols].nodes('/H/r') Vals(a)


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Memasukkan pyodbc.Binary data (BLOB) ke dalam kolom gambar SQL Server

  2. Hapus SKEMABINDING dari Tampilan di SQL Server

  3. Nilai Duplikat di Kolom Identitas

  4. Cara Memeriksa Pengaturan ANSI_NULLS Sesi Anda di SQL Server

  5. Kerangka Entitas Mengindeks SEMUA kolom kunci asing