In SQL 2005, there is no built in function that Splits the given string based upon the given delimiter character (i.e) like Split function VB6. So I came up with the following SQL Function.
----------------------------------------
CREATE FUNCTION [dbo].[Split](@input nvarchar(4000), @delimiter char(1) )
RETURNS @results TABLE(SplittedString NVARCHAR(4000) )
AS
BEGIN
DECLARE @tempInput NVARCHAR(4000)
DECLARE @position INT
DECLARE @slice NVARCHAR(4000)
IF @input IS NULL RETURN
SELECT @tempInput = @input
SELECT @position = 1
WHILE @position != 0
BEGIN
SELECT @position = CHARINDEX(@delimiter, @tempInput)
IF @position > 0
SELECT @slice = LEFT(@tempInput, @position - 1)
ELSE
SELECT @slice = @tempInput
INSERT INTO @results(SplittedString) VALUES (@slice)
SELECT @tempInput = RIGHT(@tempInput, LEN(@tempInput) - @position)
IF LEN(@tempInput) = 0 BREAK
END
RETURN
END
Sunday, October 12, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment