Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


"function"


"function"

Author
Message
aanryn
aanryn
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 55
Let the value be ‘1, 2, 3, 4……100’. How to write a function to split the value and store the numbers in the table variable. can anyone help me to solve this
Andras Belokosztolszki
Andras Belokosztolszki
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1805 Visits: 1585
CREATE FUNCTION dbo.split ( @in NVARCHAR(4000) )
RETURNS @result TABLE
( seqNr INT IDENTITY(1, 1)
, item NVARCHAR(100)
)
AS BEGIN
DECLARE @i INT
SET @i = 1

WHILE ( CHARINDEX(',', @in) > 0 )
BEGIN
INSERT INTO @result ( item )
SELECT LTRIM(RTRIM(SUBSTRING(@in, 1, CHARINDEX(',', @in) - 1)))
SET @in = SUBSTRING(@in, CHARINDEX(',', @in) + 1, LEN(@in))
SET @i = @i + 1
END

INSERT INTO @result ( item )
SELECT LTRIM(RTRIM(@in))

RETURN
END


Regards,
Andras



Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
Christopher Stobbs
Christopher Stobbs
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1910 Visits: 2232
Hi,
There are a few postings and articles for delimiting strings into tables.

here is one that I saw the last time this someone brough this topic.

hope it helps
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033

----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley

w00t
Posting Best Practices
Numbers / Tally Tables

SQL-4-Life
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2643 Visits: 23644
Using a table of numbers as in

http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html

DECLARE @str VARCHAR(100)
SET @str='1, 2, 3, 4, 5, 50, 99 ,100'


SELECT CAST(SUBSTRING(@str,
Number,
CHARINDEX(',',
@str+',',
Number)-Number) AS INT) AS Val
FROM dbo.Numbers
WHERE Number BETWEEN 1 AND LEN(@str)+1
AND SUBSTRING(','+@str,Number,1)=','


This can also be easily be done with a CLR (using a the C# 'Split' function)

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search