Last active
August 25, 2020 18:34
-
-
Save Albejr/8bed9e865aabf99c34bd82c6875c0f8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
--Object: Table-Valued Function [dbo].[fnSplitString] | |
--Script Date: 21/02/2019 16:06:34 | |
DECLARE @IDs VARCHAR(100) = '1,2,3,4' | |
SELECT * FROM [dbo].[fnSplitString](@IDs, ','); | |
*/ | |
SET ANSI_NULLS ON | |
GO | |
SET QUOTED_IDENTIFIER ON | |
GO | |
ALTER FUNCTION [dbo].[fnSplitString] | |
( | |
@string NVARCHAR(MAX), | |
@delimiter CHAR(1) | |
) | |
RETURNS @output TABLE(splitdata NVARCHAR(MAX) | |
) | |
BEGIN | |
DECLARE @start INT, @end INT | |
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) | |
WHILE @start < LEN(@string) + 1 BEGIN | |
IF @end = 0 | |
SET @end = LEN(@string) + 1 | |
INSERT INTO @output (splitdata) | |
VALUES(SUBSTRING(@string, @start, @end - @start)) | |
SET @start = @end + 1 | |
SET @end = CHARINDEX(@delimiter, @string, @start) | |
END | |
RETURN | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment