1. 程式人生 > >SQL字符串Split

SQL字符串Split

sdn 技術分享 mage style star sign def val log

GO
/****** Object:  UserDefinedFunction [dbo].[StringSplit]    Script Date: 2018/5/25 9:38:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[StringSplit]
(
    @Text NVARCHAR(MAX),
    @Sign NVARCHAR(MAX)
)
RETURNS
@ResultTable TABLE
(
    ID INT IDENTITY
(1,1) PRIMARY KEY, TextValue NVARCHAR(1024) ) AS BEGIN DECLARE @StartIndex INT DECLARE @FindIndex INT DECLARE @Content VARCHAR(4000) -- 和函數CHARINDEX有關CHARINDEX SET @StartIndex = 1 SET @FindIndex=0 WHILE(@StartIndex<=LEN(@Text)) BEGIN SET @FindIndex
=CHARINDEX(@Sign, @Text, @StartIndex) IF(@FindIndex=0 OR @FindIndex IS NULL) BEGIN -- 查找完畢 SET @FindIndex=LEN(@Text)+1 END SET @Content=LTRIM(RTRIM(SUBSTRING(@Text, @StartIndex, @FindIndex-@StartIndex))) -- 下次查找的位置 SET
@StartIndex=@FindIndex+1 -- 插入結果 IF(LEN(@Content)>0) BEGIN INSERT INTO @ResultTable(TextValue) VALUES (@Content) END END RETURN END GO

技術分享圖片

原文:

https://blog.csdn.net/ringliwei/article/details/80447066

SQL字符串Split