![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <MIN>
-- Create date: <2007-12-12>
-- Description: <分解字符串>
-- =============================================
---执行
-- SELECT * from dbo.Split_String ('a,b,c,d,we3r',',')
ALTER FUNCTION [dbo].[Split_String]
(
@split_string varchar(max), --要进行分解的字符串
@tag_string varchar(10) --分解标志
)
RETURNS
@split_table TABLE
(
split_value varchar(200)
)
AS
BEGIN
declare @temp_string varchar(max)
declare @start_index int
declare @end_index int
while 1=1
begin
set @start_index = 0
select @end_index = CharIndex(@tag_string,@split_string,@start_index)
if @end_index <> 0
begin
set @temp_string = Substring(@split_string,@start_index,@end_index)
if @temp_string is not null and @temp_string <> ''
insert into @split_table(split_value) values(@temp_string)
set @start_index = @end_index + 1
set @split_string = Substring(@split_string,@start_index,len(@split_string))
end
else
begin
if @split_string is not null and @split_string <> ''
insert into @split_table(split_value) values(@split_string)
break
end
end
RETURN
END