Declare @String varchar(max) = '1,2,3,4,5,6,7,8,9'
, @Delimiter char(1) = ','
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM (
SELECT CAST('' + REPLACE(Replace(Replace(@String,'&','&'),'<','<'), @Delimiter, ' ') + ' ' AS XML) as T1
) as XMLData
CROSS APPLY T1.nodes('/d') T (split)
here @String have 9 data, when @String have more than 2000 data at that time Query get very slow . is there any ohter solution to improve this query.
Thank you.
here @String have 9 data, when @String have more than 2000 data at that time Query get very slow . is there any ohter solution to improve this query.
Thank you.
Jignesh TrivediPosted Mar 29, 2012, 3:14 AM
That I understand but i want to know what reason behind using cast and replace?
ok,
Same thing u can acheive by fn_split which is in build in SQL-2008
Declare @String varchar(max) = '1,2,3,4,5,6,7,8,9'
, @Delimiter char(1) = ','
select Value from dbo.fn_Split(@String,@Delimiter)
And Defination of fn_split
CREATE FUNCTION [dbo].[fn_Split]
(
@ListValues nvarchar(max),
@SplitBy nvarchar(5)
)
RETURNS @ResultValue table
(
Id int identity(1,1),
Value nvarchar(100)
)
AS
BEGIN
While (Charindex(@SplitBy,@ListValues)>0)
Begin
Insert Into @ResultValue (value)
Select Value = ltrim(rtrim(Substring(@ListValues,1,Charindex(@SplitBy,@ListValues)-1)))
Set @ListValues = Substring(@ListValues,Charindex(@SplitBy,@ListValues)+len(@SplitBy),len(@ListValues))
End
Insert Into @ResultValue (Value)
Select Value = ltrim(rtrim(@ListValues))
Return
END
hope this help.
Harshil ShahPosted Mar 29, 2012, 2:55 AM
Declare @String varchar(max) = '1,2,3,4,5,6,7,8,9'
when string have more than 2000 data(1,2,3,.....,1999,2000) at that time this query get slow.
is there any solution to improve this query.
Thank you.
Jignesh TrivediPosted Mar 29, 2012, 1:44 AM
but I want what is an actual input and required out put so we can rewrite query in dirrent way to improve performance.
In first glance i can not understand what is actual out required.
brunda kPosted Mar 29, 2012, 1:32 AM
Jignesh TrivediPosted Mar 28, 2012, 11:56 PM
Can please expain what is your requirement?
Base on you Requirement we can write query.
brunda kPosted Mar 28, 2012, 11:21 PM