-- dbo.fnSplitToVarchar
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'fnSplitToVarchar')
BEGIN
DROP FUNCTION fnSplitToVarchar
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'fnSplitToVarchar')
PRINT 'DROP FUNCTION fnSplitToVarchar SUCCEEDED'
ELSE
PRINT 'DROP FUNCTION fnSplitToVarchar FAILED'
END
ELSE
PRINT 'Attempting to CREATE FUNCTION dbo.fnSplitToVarchar'
GO
CREATE FUNCTION dbo.fnSplitToVarchar
(
@SourceSql varchar(8000),
@StrSeprate varchar(10)
)
returns @temp table(splitValue varchar(500))
AS
BEGIN
DECLARE @i int
DECLARE @StrSeprateLength int
SET @StrSeprateLength = LEN(@StrSeprate)
SET @SourceSql = RTRIM(LTRIM(@SourceSql))
SET @i = CHARINDEX(@StrSeprate, @SourceSql)
WHILE @i>=1
BEGIN
IF @i=1
INSERT @temp VALUES('')
ELSE
INSERT @temp VALUES(LEFT(@SourceSql, @i-1))
SET @SourceSql = SUBSTRING(@SourceSql, @i+@StrSeprateLength, LEN(@SourceSql)-@i-@StrSeprateLength+1)
SET @i = CHARINDEX(@StrSeprate, @SourceSql)
END
IF @SourceSql<>@StrSeprate
INSERT @temp VALUES(@SourceSql)
RETURN
END
GO
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'fnSplitToVarchar')
PRINT 'CREATE FUNCTION dbo.fnSplitToVarchar SUCCEEDED'
ELSE
PRINT 'CREATE FUNCTION dbo.fnSplitToVarchar FAILED'
GO