July 22, 2011 at 3:50 am
My data is 'aaa','bbb','ccc',........
how would i display my data into separate rows using temp data..like
aaa
bbb
ccc
July 22, 2011 at 3:59 am
DECLARE @string VARCHAR(1000)
SET @string = 'aaa,bbb,ccc'
DECLARE @tblNumber TABLE
(
ID INT
)
INSERT INTO @tblNumber
SELECT TOP 100 ROW_number() OVER(order by s.object_id)
from sys.objects o,sys.objects s
select SUBSTRING(@string,id,CHARINDEX(',',@string+',',id)-id) from @tblNumber
where ID <len(@string)
AND SUBSTRING(','+@string,id,1)=','
Regards,
Mitesh OSwal
+918698619998
July 22, 2011 at 5:03 am
What you are asking for is split function
You can find samples on the web if you search with that term
Here is some
http://www.kodyaz.com/articles/sql-server-string-split-tsql-clr-function-sample.aspx
http://www.kodyaz.com/articles/sql-server-t-sql-split-function.aspx
http://www.kodyaz.com/articles/t-sql-convert-split-delimeted-string-as-rows-using-xml.aspx
July 22, 2011 at 5:15 am
Eralper (7/22/2011)
What you are asking for is split functionYou can find samples on the web if you search with that term
Here is some
http://www.kodyaz.com/articles/sql-server-string-split-tsql-clr-function-sample.aspx
http://www.kodyaz.com/articles/sql-server-t-sql-split-function.aspx
http://www.kodyaz.com/articles/t-sql-convert-split-delimeted-string-as-rows-using-xml.aspx
Oh... be careful. Splitting using a recursive CTE or XML can be mighty slow compared to other methods. If the split is being done on something that will fit into VARCHAR(8000) or NVARCHAR(4000), please consider the methods in the following article.
http://www.sqlservercentral.com/articles/Tally+Table/72993/
If you need to split one of the MAX datatypes, consider using the CLR which is also included in the "resources" near the bottom of that article.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 22, 2011 at 5:18 am
Mitesh Oswal (7/22/2011)
DECLARE @string VARCHAR(1000)
SET @string = 'aaa,bbb,ccc'
DECLARE @tblNumber TABLE
(
ID INT
)
INSERT INTO @tblNumber
SELECT TOP 100 ROW_number() OVER(order by s.object_id)
from sys.objects o,sys.objects s
select SUBSTRING(@string,id,CHARINDEX(',',@string+',',id)-id) from @tblNumber
where ID <len(@string)
AND SUBSTRING(','+@string,id,1)=','
That's the way a lot of good folks so a split. In fact, so did I. But it has a real performance problem as the number of elements increases or the width of the elements increases. The problem is with the concatenation of delimiters.
A new and much more performant method has been discovered and, yes, it's also based on a Numbers/Tally Table or an Itzik-style numbers-CTE.
Here's the link to the new methods.
http://www.sqlservercentral.com/articles/Tally+Table/72993/
--Jeff Moden
Change is inevitable... Change for the better is not.
July 22, 2011 at 5:22 am
I remember I read on a blog post where some statistics were also gathered, the CLR function had better performance values
So I agree there are different ways and methods, but CLR seems to be best for this task
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply