C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






How to get Comma seperated values as table values


Posted Date: 14 May 2008    Resource Type: Code Snippets    Category: SQL
Author: Shivshanker CheralMember Level: Diamond    
Rating: Points: 10



The following code sample shows how to get Comma seperated values as table values

CREATE FUNCTION [dbo].[fnParseArray]
(@strArray VARCHAR(8000),
@separator VARCHAR(30))

RETURNS @Parameters TABLE (i varchar(2000))

AS
BEGIN

-- @Array is the array we wish to parse
-- @Separator is the separator charactor such as a comma
declare @separator_position int -- This is used to locate each separator character
declare @array_value varchar(8000) -- this holds each array value as it is returned

-- For my loop to work I need an extra separator at the end. I always look to the
-- left of the separator character for each array value
set @strArray = @strArray + @separator

-- Loop through the string searching for separtor characters
while patindex('%' + @separator + '%' , @strArray) <> 0
begin

-- patindex matches the a pattern against a string
select @separator_position = patindex('%' + @separator + '%' , @strArray)
select @array_value = left(@strArray, @separator_position - 1)

-- This is where you process the values passed.
-- Replace this select statement with your processing
-- @array_value holds the value of this element of the array

-- select Array_Value = @array_value
insert into @Parameters
values(@array_value)

-- This replaces what we just processed with and empty string
-- if LEN(@separator)=1
-- select @strArray = stuff(@strArray, 1, @separator_position, '')
-- else
SELECT @strArray = stuff(@strArray, 1, @separator_position + (len(@separator)-1), '')


end

RETURN
END

------------------------------------------

Exmaple

SELECT * FROM dbo.fnParseArray('one,two,three,four',',')

i
---------------------------------
one
two
three
four

(4 row(s) affected)


SELECT * FROM dbo.fnParseArray('First$Second$Third$Fourth','$')

i
---------------------------------
First
Second
Third
Fourth

(4 row(s) affected)






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Comma seperated values  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: SQL Date Time important functions
Previous Resource: Simple example of full join and cross join
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use