2016-04-20 4 views
1

Meine Tabellendaten ist so etwas wie:finden Spiele in Folge

id | text 
---------------- 
1  | applicationName="App1" some text here applicationName="App2" text again applicationName="App2" 
2  | some text here applicationName="App3" some text here 
3  | some text here applicationName="App3" 

sollte Meine Ausgabe wie:

id | text 
--------------------- 
1  | App1 
1  | App2 
1  | App2 
2  | App3 
3  | App3 

Was ich versucht: ich in der Lage bin der Erste zu extrahieren passender Artikel für jede Zeile mit substring() und charindex(), aber ich bin nicht in der Lage, nachfolgende Elemente pro Zeile wie in der Ausgabe gezeigt zu finden.

Antwort

0

So können Sie versuchen, die folgende TSQL

See SQL demo here

--create table t(id int, text nvarchar(max)); 
--insert into t values 
--(1,'applicationName="App1" some text here applicationName="App2" text again applicationName="App2"'), 
--(2,'some text here applicationName="App3" some text here'), 
--(3,'some text here applicationName="App3"') 

create table #temp (rowid int,id int, text nvarchar(max)); 
create table #result (id int, data nvarchar(255)); 
insert into #temp 
    select row_number() over(order by Id desc) rowid, id,text from t; 

declare @c int, @d int, @startpos int, @endpos int 
declare @textpart nvarchar(255), @datapart nvarchar(255) 
declare @len int 
set @len =LEN('applicationName="') 
select @c=count(1) from #temp 

WHILE @c>0 
BEGIN 
    select @textpart=text from #temp where [email protected] 
    select @d= LEN(@textpart)-LEN(REPLACE(@textpart,'applicationName="','applicationName=')) 
    set @startpos=0 
    set @endpos=0 

    WHILE @d>0 
    BEGIN 
     set @startpos=CHARINDEX('applicationName="',@textpart,@startpos)[email protected] 
     set @endpos= CHARINDEX('"',@textpart,@startpos+1) 
     insert into #result 
      select @c,SUBSTRING(@textpart,@startpos,@[email protected]) 
     set @[email protected] 
     set @[email protected] 
    END 
    SET @[email protected] 
END 
select * from #result order by id asc 
drop table #temp,#result 
0

Sie müssen UDF für Ihre spezifische Anforderung erstellen. Ich habe Skript nur geschrieben, Sie testen, und es in UDF konvertieren dann verwenden Quer anwenden

DECLARE @i VARCHAR(300) = 'applicationName="App1" some text here applicationName="App2" text again applicationName="App2"' 
DECLARE @j VARCHAR(300) 
Declare @delimeter char(2)='="' 
DECLARE @t1 TABLE (col1 VARCHAR(50)) 

WHILE LEN(@i) > 0 
BEGIN 
    IF (charindex(@delimeter, @i) > 0) 
    BEGIN 
     PRINT LEN(@i) 

     SET @j = SUBSTRING(@i, charindex(@delimeter, @i) + 2, 4) 

     INSERT INTO @t1 
     VALUES (@j) 

     SET @i = stuff(@i, 1, charindex(@delimeter, @i) + 7, '') 
    END 
    ELSE 
     BREAK 
END 

SELECT * 
FROM @t1 

Dann nehme an, Sie TVF Split genannt erstellt dann wie folgt verwenden,

select *,fn.* from @t t 
cross apply(select * from dbo.split('="',t.textdata))fn