Created
November 19, 2014 17:20
-
-
Save zafergurel/94bb9d8663f6ab25370f to your computer and use it in GitHub Desktop.
T-SQL ile Virgüllü Liste Ayrıştırma ve Oluşturma (SQL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- T-SQL ile Virgüllü Liste Ayrıştırma ve Oluşturma -- | |
-- Örneklerde kullanmak üzere id alanında oluşan bir @idTable oluşturulur. | |
declare @idTable TABLE (id int) | |
insert @idTable (id) values (1) | |
insert @idTable (id) values (2) | |
/* | |
1. Virgüllü Listeye Çevirme | |
Users tablosundaki ilk 10 UserId değerini virgüllü liste haline getirme | |
*/ | |
select STUFF((select top 10 ',' + cast(id as varchar(20)) from @idTable FOR XML PATH ('')) , 1, 1, '') | |
/* | |
2. Virgüllü Listeden Sayı Tablosuna Çevirme | |
Virgüllü liste olarak gelen sayilari @idTable tablosuna girme | |
*/ | |
declare @xml xml | |
declare @ids varchar(1000) = '3,4,5,6' | |
SELECT @xml = CONVERT(xml,'<root><s>' + REPLACE(@ids,',','</s><s>') + '</s></root>') | |
insert into @idTable (id) SELECT [Value] = T.c.value('.','varchar(20)') FROM @xml.nodes('/root/s') T(c); | |
select id from @idTable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment