Kirjaudu
Google-sovellukset
Päävalikko
Post a Comment On:
mo.notono.us
"SQL: Splitting a Text Value Into a Table"
No comments yet. -
1 – 0 of 0
...And here is the SplitText function modified to use the NumberTable function: --Splits a delimited text into individual items --From
http://www.sommarskog.se/arrays-in-sql.html#tblnum-unlimited
CREATE FUNCTION dbo.SplitText( @List ntext, @Del nchar(1) = N',' ) RETURNS @T TABLE (Item nvarchar(4000)) AS BEGIN DECLARE @slices TABLE (slice nvarchar(4000) NOT NULL) DECLARE @slice nvarchar(4000), @Textpos int, @maxlen int, @stoppos int, @maxnum int SELECT @Textpos = 1, @maxlen = 4000 - 2, @maxnum = DATALENGTH(@List) / 2 WHILE DATALENGTH(@List) / 2 - (@Textpos - 1) >= @maxlen BEGIN SELECT @slice = SUBSTRING(@List, @Textpos, @maxlen) SELECT @stoppos = @maxlen - CHARINDEX(@Del, REVERSE(@slice)) INSERT @slices (slice) VALUES (@Del + LEFT(@slice, @stoppos) + @Del) SELECT @Textpos = @Textpos - 1 + @stoppos + 2 -- On the other side of the delimiter. END INSERT @slices (slice) VALUES (@Del + SUBSTRING(@List, @Textpos, @maxlen) + @Del) INSERT @T (Item) SELECT x.Item FROM ( SELECT Item = LTRIM(RTRIM(SUBSTRING(s.slice, N.Number + 1, CHARINDEX(@Del, s.slice, N.Number + 1) - N.Number - 1))) FROM dbo.NumberTable(1, @maxnum) N INNER JOIN @slices s ON N.Number <= LEN(s.slice) - 1 AND SUBSTRING(s.slice, N.Number, 1) = @Del ) AS x RETURN END
posted by Oskar Austegard at
1:40 PM
on Aug 29, 2005
Leave your comment
You can use some HTML tags, such as
<b>, <i>, <a>
Comments on this blog are restricted to team members.
Google Account
You will be asked to sign in after submitting your comment.
Please prove you're not a robot
"SQL: Splitting a Text Value Into a Table"
No comments yet. -