|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:39 PM
Points: 2,278,
Visits: 2,998
|
|
Adam Haines (2/19/2008) -------------------------------------------------------------------------------- DECLARE @IDString VARCHAR(MAX)SET @IDString =(SELECT (ID) + ','FROM MYTABLE--WHERE FILTER CAN GO HEREFOR XML PATH(''))
This leaves a trail comma. I usually do something likeDECLARE @IDString VARCHAR(MAX)SET @IDString =(SELECT CASE row_number() OVER(ORDER BY ID) WHEN 1 THEN '' ELSE ',' END + (ID)FROM MYTABLE--WHERE FILTER CAN GO HEREFOR XML PATH('')) Yes, it does leave a trailing character. I typically use a method like the one you posted, but for some reason I did not here . I mainly wanted to see if the OP was interested in using XML to generate his delimited string. I did not get a response, so I assume the method he is using is adequate for his use.
Thanks for pointing this out Derek. :)
My blog: http://jahaines.blogspot.com
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, November 24, 2008 3:47 AM
Points: 3,
Visits: 8
|
|
| Thanks guys - I don't need ot create a delimited string - the list is coming from use input (selected items in a datagrid).
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
Just did some tests.
The XML version is faster, significantly so, than the Numbers table version, for parsing out a string.
Has the added advantage of being able to take a multi-character delimiter if needed.
Of course, it won't work in SQL 2000 (correct me if I'm wrong on that), in which case the Numbers table version is the fastest I've found.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:39 PM
Points: 2,278,
Visits: 2,998
|
|
GSquared thanks for running the numbers. I know that the XML is the fastet method I have seen, but I had never gotten around to running the numbers.
My blog: http://jahaines.blogspot.com
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
In the tests I did, the XML method was slightly faster, but required less scans and reads from disk/cache. Speed differences, in many cases, were as few as 1 or 2 milliseconds. But the reduced reads and scans means less I/O bottleneck. May not matter on some systems, but worth it in many cases.
(I ran the same tests on a While loop version, and both XML and Numbers versions were consistently at least twice as fast, many times three or more times faster, and in at least one case it was impossible to judge because XML and Numbers ran in less than a millisecond but the While loop took 37 milliseconds. The While loop, on the other hand, also requires less I/O than the Numbers table. If CPU resources are less of a bottleneck on a server than I/O, and XML isn't an option, the While loop might be viable.)
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:39 PM
Points: 2,278,
Visits: 2,998
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 5:33 PM
Points: 32,902,
Visits: 26,783
|
|
GSquared (3/3/2008) Just did some tests.
The XML version is faster, significantly so, than the Numbers table version, for parsing out a string.
Any bets? ;) Post your test data please, the Numbers Table code you used for the split, and the XML code you used for the split and we'll see :)
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 5:33 PM
Points: 32,902,
Visits: 26,783
|
|
Jeff Moden (3/31/2008)
GSquared (3/3/2008) Just did some tests.
The XML version is faster, significantly so, than the Numbers table version, for parsing out a string.Any bets? ;) Post your test data please, the Numbers Table code you used for the split, and the XML code you used for the split and we'll see :)
BWAA-HAA!! Still waiting for this, Gus... :P
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Saturday, August 20, 2011 11:49 PM
Points: 1,
Visits: 0
|
|
To Run this kind of query first you need to create UDF(user defined function)
SPLIT Varchar in sql server User Defined Method CREATE FUNCTION SplitString ( -- Add the parameters for the function here @myString varchar(500), @deliminator varchar(10) ) RETURNS @ReturnTable TABLE ( -- Add the column definitions for the TABLE variable here [id] [int] IDENTITY(1,1) NOT NULL, [part] [varchar](50) NULL ) AS BEGIN Declare @iSpaces int Declare @part varchar(50)
--initialize spaces Select @iSpaces = charindex(@deliminator,@myString,0) While @iSpaces > 0
Begin Select @part = substring(@myString,0,charindex(@deliminator,@myString,0))
Insert Into @ReturnTable(part) Select @part
Select @myString = substring(@mystring,charindex(@deliminator,@myString,0)+ len(@deliminator),len(@myString) - charindex(' ',@myString,0))
Select @iSpaces = charindex(@deliminator,@myString,0) end
If len(@myString) > 0 Insert Into @ReturnTable Select @myString
RETURN END GO
Now Run this query RUN The query select * From SplitString('Mohammed**Arshad**Shaikh','**')
You may use it in where clause also.
Declare @Paramlist varchar(50) Set @Paramlist = '1,2,3'
SELECT * FROM Customer WHERE CUSTOMERId In(select * From SplitString(@paramlist,','))
This will work sure. Insha Allah..
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 5:33 PM
Points: 32,902,
Visits: 26,783
|
|
arshad7887 (8/20/2011) To Run this kind of query first you need to create UDF(user defined function)
SPLIT Varchar in sql server User Defined Method CREATE FUNCTION SplitString ( -- Add the parameters for the function here @myString varchar(500), @deliminator varchar(10) ) RETURNS @ReturnTable TABLE ( -- Add the column definitions for the TABLE variable here [id] [int] IDENTITY(1,1) NOT NULL, [part] [varchar](50) NULL ) AS BEGIN Declare @iSpaces int Declare @part varchar(50) --initialize spaces Select @iSpaces = charindex(@deliminator,@myString,0) While @iSpaces > 0 Begin Select @part = substring(@myString,0,charindex(@deliminator,@myString,0)) Insert Into @ReturnTable(part) Select @part Select @myString = substring(@mystring,charindex(@deliminator,@myString,0)+ len(@deliminator),len(@myString) - charindex(' ',@myString,0)) Select @iSpaces = charindex(@deliminator,@myString,0) end If len(@myString) > 0 Insert Into @ReturnTable Select @myString RETURN END GO Now Run this query RUN The queryselect * From SplitString('Mohammed**Arshad**Shaikh','**') You may use it in where clause also. Declare @Paramlist varchar(50) Set @Paramlist = '1,2,3' SELECT * FROM Customer WHERE CUSTOMERId In(select * From SplitString(@paramlist,',')) This will work sure. Insha Allah.. 
No... don't use a While Loop to split strings. They're just too slow. Please see the following article for the code that proves it. http://www.sqlservercentral.com/articles/Tally+Table/72993/
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|