SQL的Split函数

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  阿瑞
-- Create date: 2008-03-19
-- Description: split函数
-- Debug:select * from dbo.Fun_Split('ABC:BC:C:D:E',':')
-- =============================================
CREATE FUNCTION [dbo].[Fun_Split]
(
 @SourceSql varchar(8000),
 @StrSeprate varchar(10)
)
RETURNS
@TEMP_Table TABLE (a varchar(100))
AS
BEGIN
 DECLARE @i int
 SET @SourceSql=rtrim(ltrim(@SourceSql))
 SET @i=charindex(@StrSeprate,@SourceSql)
 WHILE @i>=1
 BEGIN
  INSERT @TEMP_Table VALUES(left(@SourceSql,@i-1))
  SET @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
  SET @i=charindex(@StrSeprate,@SourceSql)
 END
 if @SourceSql<>'\'
 INSERT @TEMP_Table values(@SourceSql)

 RETURN
END

你可能感兴趣的:(split)