拆分字符串存入表中

MS SQL 处理字符串,可以把字符串拆分为单独字符,转存入至一张表中。这样可以做到很多其它相关处理。

 

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      Insus.NET
-- Create date: 2019-05-09
-- Update date: 2019-05-09
-- Description: 拆分字符串转为表
-- =============================================

CREATE FUNCTION [dbo].[tvf_SplitStringToTable]
(
    @InputString NVARCHAR(MAX)
)
RETURNS @dump TABLE
(
    [Char] [nvarchar](2) NULL
)
AS
BEGIN    
    DECLARE @position INT = 1, @strLen INT = LEN(@InputString)
    WHILE @position <= @strLen
    BEGIN
       INSERT INTO @dump VALUES(SUBSTRING(@InputString, @position, 1))
        SET @position = @position + 1
    END
    RETURN
END
GO
Source Code

 

举个例子:

 

posted @ 2019-05-09 08:04  Insus.NET  阅读(608)  评论(0编辑  收藏  举报