Split a string using sql function

Rajni.Shekhar
Posted by Rajni.Shekhar under Sql Server category on | Points: 40 | Views : 1628
CREATE FUNCTION funSplit 
(
@Delimiter VARCHAR(5),
@StringValue VARCHAR(8000)
)
RETURNS @TableOfValues TABLE
(
RowID SMALLINT IDENTITY(1,1),
[Value] VARCHAR(150)
)
AS
BEGIN

DECLARE @LenString INT

WHILE LEN( @StringValue ) > 0
BEGIN
SELECT @LenString = (CASE CHARINDEX(@Delimiter, @StringValue) WHEN 0 THEN LEN(@StringValue )
ELSE (CHARINDEX(@Delimiter,@StringValue) -1) END)

INSERT INTO @TableOfValues SELECT SUBSTRING(@StringValue, 1, @LenString)

SELECT @StringValue = (CASE (LEN(@StringValue) - @LenString) WHEN 0 THEN ''
ELSE RIGHT(@StringValue,LEN(@StringValue) - @LenString - 1 ) END)
END

RETURN

END

Comments or Responses

Posted by: T.Saravanan on: 4/2/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Kindly post your code inside the code tag.
Posted by: Rajni.Shekhar on: 4/3/2012 Level:Bronze | Status: [Member] | Points: 10
Sorry, i am not getting you, which code tag you are talking about. Please guide.
Posted by: T.Saravanan on: 4/3/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

This is simple.
On code submission, follow the below steps..
1. Select your code from contents section.
2. Click the code tab. i.e, afterParagraph Title
3. Finally submit your code.

Its look like below...

Your Code...


Kindly let me know if you not understand.
Posted by: Rajni.Shekhar on: 4/3/2012 Level:Bronze | Status: [Member] | Points: 10
Thanks to guide me, i didnt notice code tag there :)

Login to post response