Hi,
As Limno suggested , yes it is not good store values like that in a column as far as normalization point of view, but if you are storing here is the test code to split the values
Go
CREATE FUNCTION [fn_Split](@text VARCHAR(MAX), @delimiter VARCHAR(20) = ' ')
RETURNS
@Strings TABLE ( [position] INT IDENTITY PRIMARY KEY , [value] varchar(100))
AS
BEGIN
DECLARE @index int
SET @index = -1
WHILE (LEN(@text) > 0)
BEGIN -- Find the first delimiter
SET @index = CHARINDEX(@delimiter , @text)
--No delimiter left?
--Insert the remaining @text and break the loop
IF (@index = 0) AND (LEN(@text) > 0)
BEGIN
INSERT INTO @Strings
VALUES (CAST(@text AS varchar(100)))
BREAK
END
-- Found a delimiter
--Insert left of the delimiter and truncate the @text
IF (@index > 1)
BEGIN
INSERT INTO @Strings
VALUES (CAST(LEFT(@text, @index - 1) AS varchar(100)))
SET @text = RIGHT(@text, (LEN(@text) - @index))
END --Delimiter is 1st position = no @text to insert
ELSE
SET @text = CAST(RIGHT(@text, (LEN(@text) - @index)) AS varchar(100))
END RETURN
END
GO
--To Test It
GO
DECLARE @commasprStr AS VARCHAR(50)
SET @commasprStr='Abc,Def,Ghi,Jkl'
SELECT * FROM [dbo].[fn_Split](@commasprStr,',')
GO
--OUTPUT
--position value
------------- ---------
--1 Abc
--2 Def
--3 Ghi
--4 Jkl
Raghav
CodeASP.NET Community |
My Blog |
jQuery Intellisense in Visual Studio 2008 "Success doesn't come to you…you go to it."--Marva Collins
"Failure is success if we learn from it." Malcolm Forbes
"Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous