all groups > sql server programming > september 2006 >
You're in the

sql server programming

group:

splitting up a string into seperate values


Re: splitting up a string into seperate values SQL Menace
9/16/2006 12:26:13 PM
sql server programming:
First create this number table

CREATE TABLE NumberPivot (NumberID INT PRIMARY KEY)
DECLARE @intLoopCounter INT
SELECT @intLoopCounter =0
WHILE @intLoopCounter <=999 BEGIN
INSERT INTO NumberPivot
VALUES (@intLoopCounter)
SELECT @intLoopCounter = @intLoopCounter +1
END


then your proc will be this, change YourTable to your table name

CREATE PROC sp_addfavstocustomer
@id INT,
@favs NVARCHAR(200)
AS
SET NOCOUNT ON

INSERT INTO YourTable
SELECT @id ,SUBSTRING(',' + @favs + ',', NumberID + 1,
CHARINDEX(',', ',' + @favs + ',', NumberID + 1) - NumberID -1)AS Value
FROM NumberPivot
WHERE NumberID <= LEN(',' + @favs + ',') - 1
AND SUBSTRING(',' + @favs + ',', NumberID, 1) = ','
SET NOCOUNT OFF
GO


Denis the SQL Menace
http://sqlservercode.blogspot.com/


[quoted text, click to view]
splitting up a string into seperate values Aussie Rules
9/16/2006 8:02:19 PM
Hi,

To save numerous round trips between the server and client, I want to pass
in a string that contains a number of values, that the store proc will split
up into the single values and insert into a table.

For example, the client will execute the following

exec sp_addfavstoCustomer
1,'1,5,33,21,454,22,12,432,443,2,1,3,4,5,3,2,4,,8,9,9,0,8,7,99)

The stored proc is

create proc sp_addfavstocustomer
@id int,
@favs nvarchar(200)

as.....

The idea would be insert into a table the following records

1,1
1,5
1,33
1,21
etc

The first value (1) is the customer number, the second is the next value
from the collection of numbers held in the string... the second value is a
int field..

Is there an easy way to do this ?

Thanks


Re: splitting up a string into seperate values Erland Sommarskog
9/16/2006 10:20:16 PM
Aussie Rules (AussieRules@nospam.nospam) writes:
[quoted text, click to view]

Never call your procedures sp_something. That prefix is reserved for
system procedures, and SQL Server will first look in master for
these.

[quoted text, click to view]

See http://www.sommarskog.se/arrays-in-sql.html#iter-list-of-integers.
Read the full article if you want to try other methods.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: splitting up a string into seperate values Tracy McKibben
9/18/2006 8:15:11 AM
[quoted text, click to view]

http://realsqlguy.com/serendipity/archives/4-All-Strung-Out.html


--
Tracy McKibben
MCDBA
AddThis Social Bookmark Button