Trennzeichen getrennten String sortieren

Manchmal braucht man Dinge...

if object_id('dbo.OrderStringparts') is not NULL drop function dbo.OrderStringparts

go

create function dbo.OrderStringparts

(

@String nvarchar(max),

@Delimiter nvarchar(max)

)

returns nvarchar(max)

as

begin

declare @out nvarchar(max);

with tab(Part) as -- http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum

(

select

substring(@String, n, charindex(@delimiter, @String + @delimiter, n) - n)

from dbo.Numbers(1, len(@String)) -- http://stevekass.com/2006/06/03/how-to-generate-a-sequence-on-the-fly

where

substring(@delimiter + @String, n, len(replace(@delimiter, ' ', '_'))) = @delimiter

),

string(string) as

(

select

@Delimiter + Part

from tab

order by

Part

for xml path('')

)

select

@out = substring(string, 2, 2147483647)

from string

return @out

end

go

 

Viele Grüße

Christoph Ingenhaag

Noch kein Feedback
Einen Kommentar hinterlassen

In order to leave a comment Jetzt einloggen!