A string split function for SQL Server
2008-12-07 03:03
836 查看
This is my string split function for SQL Server.
-- ================================================
-- Returns a table that contains the substrings in the source string that are delimited by the specified character.
-- Parameters specify the maximum number of substrings to return and whether to return empty strings.
-- ================================================
ALTER FUNCTION [dbo].[ufn_Split]
(
-- The source string to split.
@StringToSplit NVARCHAR(MAX),
-- The character that delimit the substrings,
-- NULL contains no delimiters.
@Separator NCHAR(1),
-- The maximum number of substrings to return.
@ReturnCount INT = NULL,
-- Specify 0 or NULL to omit empty strings in the table returned,
-- otherwise to include empty strings from the table returned.
@SplitOption BIT = 1
)
RETURNS @ReturnTable TABLE
(
[String] NVARCHAR(MAX)
)
AS
BEGIN
-- Test the @StringToSplit and @ReturnCount parameters
IF ((@StringToSplit IS NULL) OR (LEN(@StringToSplit) = 0))
OR ((@ReturnCount IS NOT NULL) AND (@ReturnCount <= 0))
GOTO FUNCTION_END;
-- Test the @Separator parameter
IF (@Separator IS NULL) OR (LEN(@Separator) = 0)
BEGIN
INSERT @ReturnTable VALUES (@StringToSplit);
GOTO FUNCTION_END;
END
-- Split the source string
DECLARE @Result TABLE ([String] NVARCHAR(MAX));
DECLARE @Length AS INT = LEN(@StringToSplit),
@Start AS INT = 0;
WHILE @Start <= @Length
BEGIN
DECLARE @Next AS INT = CHARINDEX(@Separator, @StringToSplit, @Start);
DECLARE @Sub NVARCHAR(MAX);
IF @Next >= 1
BEGIN
SET @Sub = SUBSTRING(@StringToSplit, @Start, @Next - @Start);
INSERT @Result VALUES (@Sub);
SET @Start = @Next + 1;
END
ELSE
BEGIN
IF @Start <= @Length
BEGIN
SET @Sub = SUBSTRING(@StringToSplit, @Start, @Length - @Start);
INSERT @Result VALUES (@Sub);
END
BREAK
END
END
IF SUBSTRING(@StringToSplit, @Length, 1) = @Separator
INSERT INTO @Result VALUES ('');
-- Clean results according to the @SplitOption parameter
IF (@SplitOption = 0) OR (@SplitOption IS NULL)
DELETE FROM @Result WHERE [String] = '';
-- Limit the return count
DECLARE @ResultCount AS INT;
SELECT @ResultCount = COUNT([String]) FROM @Result;
IF (@ReturnCount IS NOT NULL) AND (@ResultCount > @ReturnCount)
SET @ResultCount = @ReturnCount;
-- Add results to the to be returned table
DECLARE @String AS NVARCHAR(MAX);
DECLARE ResultCursor CURSOR FOR SELECT * FROM @Result;
OPEN ResultCursor;
FETCH NEXT FROM ResultCursor INTO @String;
WHILE (@@FETCH_STATUS = 0) AND (@ResultCount > 0)
BEGIN
INSERT @ReturnTable VALUES (@String);
FETCH NEXT FROM ResultCursor INTO @String;
SET @ResultCount = @ResultCount - 1;
END
CLOSE ResultCursor;
DEALLOCATE ResultCursor;
-- Return the function
FUNCTION_END:
RETURN;
END
-- ================================================
-- Returns a table that contains the substrings in the source string that are delimited by the specified character.
-- Parameters specify the maximum number of substrings to return and whether to return empty strings.
-- ================================================
ALTER FUNCTION [dbo].[ufn_Split]
(
-- The source string to split.
@StringToSplit NVARCHAR(MAX),
-- The character that delimit the substrings,
-- NULL contains no delimiters.
@Separator NCHAR(1),
-- The maximum number of substrings to return.
@ReturnCount INT = NULL,
-- Specify 0 or NULL to omit empty strings in the table returned,
-- otherwise to include empty strings from the table returned.
@SplitOption BIT = 1
)
RETURNS @ReturnTable TABLE
(
[String] NVARCHAR(MAX)
)
AS
BEGIN
-- Test the @StringToSplit and @ReturnCount parameters
IF ((@StringToSplit IS NULL) OR (LEN(@StringToSplit) = 0))
OR ((@ReturnCount IS NOT NULL) AND (@ReturnCount <= 0))
GOTO FUNCTION_END;
-- Test the @Separator parameter
IF (@Separator IS NULL) OR (LEN(@Separator) = 0)
BEGIN
INSERT @ReturnTable VALUES (@StringToSplit);
GOTO FUNCTION_END;
END
-- Split the source string
DECLARE @Result TABLE ([String] NVARCHAR(MAX));
DECLARE @Length AS INT = LEN(@StringToSplit),
@Start AS INT = 0;
WHILE @Start <= @Length
BEGIN
DECLARE @Next AS INT = CHARINDEX(@Separator, @StringToSplit, @Start);
DECLARE @Sub NVARCHAR(MAX);
IF @Next >= 1
BEGIN
SET @Sub = SUBSTRING(@StringToSplit, @Start, @Next - @Start);
INSERT @Result VALUES (@Sub);
SET @Start = @Next + 1;
END
ELSE
BEGIN
IF @Start <= @Length
BEGIN
SET @Sub = SUBSTRING(@StringToSplit, @Start, @Length - @Start);
INSERT @Result VALUES (@Sub);
END
BREAK
END
END
IF SUBSTRING(@StringToSplit, @Length, 1) = @Separator
INSERT INTO @Result VALUES ('');
-- Clean results according to the @SplitOption parameter
IF (@SplitOption = 0) OR (@SplitOption IS NULL)
DELETE FROM @Result WHERE [String] = '';
-- Limit the return count
DECLARE @ResultCount AS INT;
SELECT @ResultCount = COUNT([String]) FROM @Result;
IF (@ReturnCount IS NOT NULL) AND (@ResultCount > @ReturnCount)
SET @ResultCount = @ReturnCount;
-- Add results to the to be returned table
DECLARE @String AS NVARCHAR(MAX);
DECLARE ResultCursor CURSOR FOR SELECT * FROM @Result;
OPEN ResultCursor;
FETCH NEXT FROM ResultCursor INTO @String;
WHILE (@@FETCH_STATUS = 0) AND (@ResultCount > 0)
BEGIN
INSERT @ReturnTable VALUES (@String);
FETCH NEXT FROM ResultCursor INTO @String;
SET @ResultCount = @ResultCount - 1;
END
CLOSE ResultCursor;
DEALLOCATE ResultCursor;
-- Return the function
FUNCTION_END:
RETURN;
END
相关文章推荐
- SqlServer Function Split 2
- A SQL Function which used to split string by char
- Sqlserver Split function
- Split string in SQL Server 2005+ CLR vs. T-SQL(转载)
- 【原】[SQL function] SQL Server Split Function
- SqlServer Split Function
- SqlServer Function(SplitText)
- The Split Function -- use for std::string
- SQL SplitString function
- Solution: Performance Dashboard Reports for SQL Server 2008 Can not install
- 灵活运用 SQL SERVER FOR XML PATH
- Oracle学习系列—Oracle透明网关 for SQL Server
- vs2010未能正确加载vsts for database professional sql server data-tier application这个错误
- Solutions for INSERT OR UPDATE on SQL Server
- mysql 错误 SQL Error: 1366: Incorrect string value: "\xE8\xAF\xA6\xE7\xBB\x86…" for column "address" a
- java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA\xB9\xA4...' for column 'con
- 关于添加Adventure Works for SQL Server 2012出错
- Element type is invalid: expected a string (for built-in components) or a class/function (for compos
- SQL Server Migration Assistant for MySQL使用方法 数据迁移
- [转]ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB