Principale utente con più risposte
Splittare una stringa con piu separatori in una tabella con piu colonne

Domanda
-
salve ragazzi
io "dovrei" splittare una stringa con 2 tipi di separatori esempio "," e" ;" in una tabella di 3 colonne tipo cosi
tabella
create table #pippo
(
col1 int,
col2 int,
col3 int
)
e passare una stringa tipo questa '1,2,3;4,5,6;7,8,9;' eccc
il risultato dovrebbe essere
col1 col2 col3
1 2 3
4 5 6
7 8 9
è possibile fare questo?
grazie
Risposte
-
Ops. Scusa, oltre a splittare la stringa (in questo caso utilizzando due separatori diversi) occorreva anche combinare il risultato in colonne.
Se il numero di colonne è fisso (3 nel tuo caso), puoi usare il codice qui sotto:
HTHUSE tempdb; GO CREATE FUNCTION [dbo].[fn_Split] ( @String varchar(8000), @Delimiter char(1) ) RETURNS @TempTable TABLE (itemid int identity,items varchar(8000)) as begin declare @intPosition int, @vchElement varchar(8000) if len(@String) < 1 or @String is null return set @intPosition = 1 while @intPosition <> 0 begin set @intPosition = charindex(@Delimiter, @String) if @intPosition <> 0 set @vchElement = left(@String, @intPosition - 1) else set @vchElement = @String if len(@vchElement) > 0 insert into @TempTable(items) values(@vchElement) set @String = right(@String, len(@String) - @intPosition) if len(@String) = 0 break end return end go declare @String varchar(max) = '1,2,3;4,5,6;7,8,9;' select max(case when f2.itemid = 1 then f2.items end) AS Col1, max(case when f2.itemid = 2 then f2.items end) AS Col2, max(case when f2.itemid = 3 then f2.items end) AS Col3 from dbo.fn_Split(@String, ';') f1 cross apply dbo.fn_Split(f1.items, ',') f2 group by f1.itemid;
Risultato:
Col1 Col2 Col3
----------------
1 2 3
4 5 6
7 8 9Danilo Dominici MCP MCDBA MCITP MCSE MCAD Questo post è fornito "così com'è". Non conferisce garanzie o diritti di alcun tipo. Ricorda di usare la funzione "segna come risposta" per i post che ti hanno aiutato a risolvere il problema e "deseleziona come risposta" quando le risposte segnate non sono effettivamente utili. Questo è particolarmente utile per altri utenti che leggono il thread, alla ricerca di soluzioni a problemi similari. ENG: This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
- Contrassegnato come risposta brggpr mercoledì 20 giugno 2012 14:30
Tutte le risposte
-
Ciao,
prova questa funzione di Simon Greener: http://www.spatialdbadvisor.com/sql_server_blog/136/string-tokenizer-for-sql-server-2008-written-in-tsql
o questa di Itai Goldstein: http://blogs.microsoft.co.il/blogs/itai/archive/2009/02/01/t-sql-split-function.aspx
Danilo Dominici MCP MCDBA MCITP MCSE MCAD Questo post è fornito "così com'è". Non conferisce garanzie o diritti di alcun tipo. Ricorda di usare la funzione "segna come risposta" per i post che ti hanno aiutato a risolvere il problema e "deseleziona come risposta" quando le risposte segnate non sono effettivamente utili. Questo è particolarmente utile per altri utenti che leggono il thread, alla ricerca di soluzioni a problemi similari. ENG: This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
-
-
Ops. Scusa, oltre a splittare la stringa (in questo caso utilizzando due separatori diversi) occorreva anche combinare il risultato in colonne.
Se il numero di colonne è fisso (3 nel tuo caso), puoi usare il codice qui sotto:
HTHUSE tempdb; GO CREATE FUNCTION [dbo].[fn_Split] ( @String varchar(8000), @Delimiter char(1) ) RETURNS @TempTable TABLE (itemid int identity,items varchar(8000)) as begin declare @intPosition int, @vchElement varchar(8000) if len(@String) < 1 or @String is null return set @intPosition = 1 while @intPosition <> 0 begin set @intPosition = charindex(@Delimiter, @String) if @intPosition <> 0 set @vchElement = left(@String, @intPosition - 1) else set @vchElement = @String if len(@vchElement) > 0 insert into @TempTable(items) values(@vchElement) set @String = right(@String, len(@String) - @intPosition) if len(@String) = 0 break end return end go declare @String varchar(max) = '1,2,3;4,5,6;7,8,9;' select max(case when f2.itemid = 1 then f2.items end) AS Col1, max(case when f2.itemid = 2 then f2.items end) AS Col2, max(case when f2.itemid = 3 then f2.items end) AS Col3 from dbo.fn_Split(@String, ';') f1 cross apply dbo.fn_Split(f1.items, ',') f2 group by f1.itemid;
Risultato:
Col1 Col2 Col3
----------------
1 2 3
4 5 6
7 8 9Danilo Dominici MCP MCDBA MCITP MCSE MCAD Questo post è fornito "così com'è". Non conferisce garanzie o diritti di alcun tipo. Ricorda di usare la funzione "segna come risposta" per i post che ti hanno aiutato a risolvere il problema e "deseleziona come risposta" quando le risposte segnate non sono effettivamente utili. Questo è particolarmente utile per altri utenti che leggono il thread, alla ricerca di soluzioni a problemi similari. ENG: This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
- Contrassegnato come risposta brggpr mercoledì 20 giugno 2012 14:30