Ich habe eine SQL-Tabelle, in der ich zwei Felder als No
und declaration
habe.
Code Declaration
123 a1-2 nos, a2- 230 nos, a3 - 5nos
Ich muss die Deklaration für diesen Code anzeigen als:
Code Declaration
123 a1 - 2nos
123 a2 - 230nos
123 a3 - 5nos
Ich muss die Spaltendaten in Zeilen für diesen Code aufteilen.
Für diese Art der Datentrennung würde ich vorschlagen, eine Splitfunktion zu erstellen:
create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))
returns @temptable TABLE (items varchar(MAX))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end;
Um dies in einer Abfrage zu verwenden, können Sie einen outer apply
verwenden, um sich mit Ihrer vorhandenen Tabelle zu verbinden:
select t1.code, s.items declaration
from yourtable t1
outer apply dbo.split(t1.declaration, ',') s
Welches führt zum Ergebnis:
| CODE | DECLARATION |
-----------------------
| 123 | a1-2 nos |
| 123 | a2- 230 nos |
| 123 | a3 - 5nos |
Siehe SQL Fiddle mit Demo
Oder Sie können eine CTE-Version ähnlich der folgenden implementieren:
;with cte (code, DeclarationItem, Declaration) as
(
select Code,
cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
from yourtable
union all
select code,
cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
from cte
where Declaration > ''
)
select code, DeclarationItem
from cte
Declare @t Table([Code] int, [Declaration] varchar(32));
Insert Into @t([Code], [Declaration])
Values(123, 'a1-2 nos, a2- 230 nos, a3 - 5nos')
Select
x.[Code]
,t.Declaration
From
(
Select
*,
Cast('<X>'+Replace(t.[Declaration],',','</X><X>')+'</X>' As XML) As record
From @t t
)x
Cross Apply
(
Select fdata.D.value('.','varchar(50)') As Declaration
From x.record.nodes('X') As fdata(D)
) t
Ein paar Mal zurück, ich habe ungefähr das gleiche gebloggt Split-Funktion in SQL Server mit Set base approach
Besuchen Sie auch Erland Sommarskog blog, der die Antwort seit 15 Jahren aufrechterhält.
Versuche dies....
declare @col1 varchar(100),@CurentSubString varchar(100)
create table #temp
(
col1 varchar(50)
)
DECLARE CUR CURSOR
FOR SELECT col1
FROM your_table
open CUR
FETCH next
FROM CUR
INTO @col1
WHILE @@FETCH_STATUS = 0
BEGIN
WHILE CHARINDEX (@col1, ';') <> 0
BEGIN
SET @CurentSubString = SUBSTRING(@col1,1,CHARINDEX (@col1, ';'))
SET @col1 = SUBSTRING(@col1,CHARINDEX (@col1, ';')+1,len(@col1))
insert into #temp
select @CurentSubString
END
IF CHARINDEX (@col1, ';') = 0 and isnull(@col1,'')!= ''
BEGIN
INSERT INTO #temp
SELECT @col1
END
FETCH next
FROM CUR
INTO @col1
END
select *
From #temp
CLOSE CUR
DEALLOCATE CUR