|
|
|
Sometimes we may need to store values in one field using some sort of seperating character. For example if you want to store multiple values in one field instead of normalizing your table and creating a whole new table to store multiple records. This articles will show you how you can create a UDF which can return table from a string which is CSV (Comma seperated values) stored in one field. By doing this you reduce complexity of your system.
Here is the user defined function |
Click here to copy the following block |
CREATE FUNCTION ParseArray (@Array varchar(8000), @separator char) RETURNS @ParsedArrays TABLE (row_counter int identity(1,1), array_value varchar(8000)) AS BEGIN declare @separator_position int declare @array_value varchar(8000) set @array = @array + @separator while patindex('%' + @separator + '%' , @array) <> 0 begin select @separator_position = patindex('%' + @separator + '%' , @array) select @array_value = left(@array, @separator_position - 1) insert @ParsedArrays(array_value) VALUES (@array_value) select @array = stuff(@array, 1, @separator_position, '') end RETURN END GO
select * from dbo.ParseArray('1|2|3|4','|') as tmp |
array_value
-------------------------
1
2
3
4
(4 row(s) affected) |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|