Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 29th, 2006, 07:34 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default Having trouble splitting a list to a resultset?

Ok, run this...

SELECT * FROM dbo.fnListSplit('a,10,john.doe#whitehouse.gov,john doe,green,president', ',')

-------------------------------------------------------------
CREATE FUNCTION dbo.fnListSplit
(
    @List VARCHAR(8000),
    @Delimiter VARCHAR(1)
)
RETURNS @Resultset TABLE (i SMALLINT IDENTITY(0, 1), x VARCHAR(50))
AS

BEGIN
    INSERT @Resultset
            (
                x
            )
    SELECT SUBSTRING(@Delimiter + @List + @Delimiter, w.i + 1, CHARINDEX(@Delimiter, @Delimiter + @List + @Delimiter, w.i + 1) - w.i - 1)
    FROM (
                SELECT v0.n + v1.n i
                FROM (
                        SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
                    )v0,
                    (
                        SELECT 0 n UNION ALL SELECT 16 UNION ALL SELECT 32 UNION ALL SELECT 48 UNION ALL SELECT 64 UNION ALL SELECT 80 UNION ALL SELECT 96 UNION ALL SELECT 112 UNION SELECT 128 UNION ALL SELECT 144 UNION ALL SELECT 160 UNION ALL SELECT 176 UNION ALL SELECT 192 UNION ALL SELECT 208 UNION ALL SELECT 224 UNION ALL SELECT 240
                    ) v1
            ) w
    WHERE w.i = CHARINDEX(@Delimiter, @Delimiter + @List + @Delimiter, w.i) AND w.i < LEN(@Delimiter + @List)
    ORDER BY w.i

    RETURN
END
 
Old May 30th, 2006, 12:26 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
Default

DONE.

I am getting the output as
---------
i | x
---------
0 a
1 b
2 c
3 d
4 e
5 f
--------
But, I did'nt get your problem, what exactly you are looking for.

hope to see information from you.


With Regards,
Raghavendra Mudugal
 
Old May 30th, 2006, 02:36 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I wasn't looking for anything. Just helping out a lot of programmers with a problem that is often recurring.
 
Old May 30th, 2006, 10:36 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Peso,

Thank you for this and other posts. Sharing info is most appreciated. This function will split up to 26 items???

Richard

 
Old May 30th, 2006, 10:58 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by rstelma
 Peso,

Thank you for this and other posts. Sharing info is most appreciated. This function will split up to 26 items???

Richard
Yes, the function above will split at most 256 values. If you don't need so many values, here is another example that allows up to 16 splits.

CREATE FUNCTION dbo.fnListSplit
(
    @List VARCHAR(850),
    @Delimiter VARCHAR(1)
)
RETURNS @Resultset TABLE (i SMALLINT IDENTITY(0, 1), x VARCHAR(50))
AS

BEGIN
    INSERT @Resultset
            (
                x
            )
    SELECT SUBSTRING(@Delimiter + @List + @Delimiter, w.i + 1, CHARINDEX(@Delimiter, @Delimiter + @List + @Delimiter, w.i + 1) - w.i - 1)
    FROM (
                       SELECT 0 i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
            ) w
    WHERE w.i = CHARINDEX(@Delimiter, @Delimiter + @List + @Delimiter, w.i) AND w.i < LEN(@Delimiter + @List)
    ORDER BY w.i

    RETURN
END

For example, say you want the third and fourth value in the list.

Just write

SELECT x FROM dbo.fnListSplit('a,b,c,d,e,f', ',') where i = 3 or i = 4






Similar Threads
Thread Thread Starter Forum Replies Last Post
Resultset error madhucm JSP Basics 0 August 4th, 2005 12:08 PM
Question in ResultSet.TYPE_FORWARD_ONLY viks Java Databases 1 March 27th, 2005 05:18 AM
Trouble putting data into Excel Drop Down List Arsi C# 2 October 26th, 2004 11:47 AM
Find a particular data using ResultSet bamboat_45 Java Databases 2 August 5th, 2004 12:54 AM
Splitting a list of asp pages mjwtaylor Classic ASP Basics 1 October 14th, 2003 07:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.