SQL 字符串拆分
字符串拆分:
ALTER FUNCTION [dbo].[f_Split](@sText nvarchar(max),@split NVARCHAR(20)) RETURNS @t TABLE (idx INT IDENTITY(1,1) Primary Key , [value] nvarchar(4000)) AS BEGIN DECLARE @temp NVARCHAR(1000) WHILE(CHARINDEX(@split,@sText)<>0) BEGIN SET @temp = SUBSTRING(@sText,1,CHARINDEX(@split,@sText)-1) IF(@temp <> 'Empty' AND @temp <> ' ') INSERT @t([value]) VALUES (@temp) SET @sText = STUFF(@sText,1,CHARINDEX(@split,@sText),'') END IF(@sText <> 'Empty' AND @sText <> ' ') INSERT @t([value]) VALUES (@sText) RETURN END
使用方法:
DECLARE @HouseCodeStr NVARCHAR(MAX) SET @HouseCodeStr ='26a24d5a-9a50-4503-9579-e01c6c01aefd,9980eda0-43da-400e-baf9-2a2e1c7b1dd1,570beb03-99e9-4d8c-8a4f-782cf2428073,ef861f42-ae8b-4045-94c3-eb9907ba643f,35ad978e-9b17-4067-b606-262917a81258,4517b7f6-b0ea-4661-95ab-f36d5d8c82cb,2c606205-9fd9-4d68-a649-2273c5ec0dd8' SELECT * FROM f_Split(@HouseCodeStr, ',')