Online Earning Sources (Without Investment)

If you want to post, send your post on dotnetglobe@gmail.com .Put 'Title' as 'Subject'

Pages

Tuesday, March 15, 2011

Split the String

Copy & Paste below script into Query window and execute it

/*------------Create Split function--------------------*/
CREATE FUNCTION [dbo].[Split]
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
value nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1

While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (value)
Select
value = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End

Insert Into @RtnValue (value)
Select value = ltrim(rtrim(@RowData))

Return
END


/*-------------How toUse--------------*/
declare @string varchar(200)
set @string='a,b,c,d,e,f,g'
select value from dbo.Split(@string,',')

No comments:

itworld