SQL----类似Split函数的实现1

CREATE FUNCTION dbo.Split(@Str VARCHAR(8000))
RETURNS
@List TABLE
(
OBJECT VARCHAR(100)
)
AS
BEGIN
DECLARE @OBJECT AS VARCHAR(100)
WHILE CHARINDEX(',',@Str)>0
BEGIN
 SET @OBJECT=SUBSTRING(@Str,1,CHARINDEX(',',@Str)-1)
 INSERT INTO @List
 (
  OBJECT
 )
 SELECT
  @OBJECT
 SET @Str=STUFF(@Str,1,CHARINDEX(',',@Str),'')
END
RETURN
END
------------------------------------------------------------------------
DECLARE @STR VARCHAR(500)
SET @STR='2009/01/01,2010/05/01,'
SELECT CONVERT(DATETIME,OBJECT) FROM Split(@STR)----DATETIEM
SET @STR='1,2,3,4,5,6,7,8,9,10'
SELECT CONVERT(INT,OBJECT) FROM Split(@STR)------INT

你可能感兴趣的:(SQL----类似Split函数的实现1)