/*
Description : 将字符串按指定字符分隔后存储到table
SELECT * FROM dbo.[SplitToTable_BySpecifySymbol]('abcd,edfadk,kjlfkj',',')
*/
CREATE FUNCTION [dbo].[SplitToTable_BySpecifySymbol]
( @InputString nvarchar(MAX),
@Symbol varchar(1)
)
RETURNS
@tblReturn TABLE (COL1 nvarchar(60))
AS
BEGIN
DECLARE @CIndex smallint
WHILE (@InputString<>'' and @Symbol <>'')
BEGIN
SET @CIndex=CHARINDEX(@Symbol,@InputString)
IF @CIndex=0 SET @CIndex=LEN(@InputString)+1
--透過substring函數取得第一個字串,並輸入資料表變數中
INSERT INTO @tblReturn (COL1)
VALUES (SUBSTRING(@InputString,1,@CIndex-1))
IF @CIndex=LEN(@InputString)+1 BREAK
SET @InputString=SUBSTRING(@InputString,@CIndex+1,LEN(@InputString)-@CIndex)
END
RETURN
END
SQL Server 将字符串按指定字符分隔后存储到table
最新推荐文章于 2023-10-23 09:33:51 发布