MS SQL 處理字元串,可以把字元串拆分為單獨字元,轉存入至一張表中。這樣可以做到很多其它相關處理。 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- -- Author: Insus.NET -- Create date: 2019-05-0 ...
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 GOSource Code
舉個例子: