Friday, June 25, 2010

SQL Split Function

I was just trying to do a split in a stored procedure, and found this
link. This is really helpfull comparing with other resources available on the web.

http://sqltutorials.blogspot.com/2007/09/sql-function-split.html

Below is Split Function in SQL

DECLARE @NextString NVARCHAR(40)
DECLARE @Pos INT
DECLARE @NextPos INT
DECLARE @String NVARCHAR(40)
DECLARE @Delimiter NVARCHAR(40)

SET @String ='SQL,TUTORIALS'
SET @Delimiter = ','
SET @String = @String + @Delimiter
SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String,@pos+1,len(@String))
SET @pos = charindex(@Delimiter,@String)
END

Result
- SQL
- TUTORIALS

0 comments:

My Achievements

Member of

Blog Archive

Followers