Split String

CREATEsplitstring FUNCTION [dbo].[SplitString] (
@myString nvarchar(max),
@deliminator nvarchar(10)
)
RETURNS @ReturnTable TABLE (
[id] [int] IDENTITY(1,1) NOT NULL,
[part] [nvarchar](max) NULL
)
AS BEGIN
Declare @iSpaces int
Declare @part nvarchar(max)
Set @myString = REPLACE(@myString,CHAR(39),CHAR(165))
Select @iSpaces = charindex(@deliminator,@myString,0)
While @iSpaces > 0
Begin
Select @part = substring(@myString,0,charindex(@deliminator,@myString,0))
Insert Into @ReturnTable(part)
Select REPLACE(@part,CHAR(165),CHAR(39))
Select @myString = substring(@mystring,charindex(@deliminator,@myString,0)+ len(@deliminator),len(@myString))
Select @iSpaces = charindex(@deliminator,@myString,0)
end
If len(@myString) > 0
Insert Into @ReturnTable
Select REPLACE(@myString,CHAR(165),CHAR(39))
RETURN
END

Run : select * from dbo.SplitString(‘mani-kandan’,’-‘)

Result :

id          part
1            mani
2            kandan

Advertisements

Author: tdmsql

I am a PL-Sql Developer. I am searching some sites sql studies but its bored. i like create studies note my own way. so that i created this site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s