﻿USE [FabApprovalSystem]
GO
	/****** Object:  UserDefinedFunction [dbo].[fnSplitCSV]    Script Date: 11/21/2024 11:31:55 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE FUNCTION [dbo].[fnSplitCSV](@csv varchar(max)) RETURNS @r TABLE (Position int, Val varchar(max)) AS BEGIN DECLARE @I INT = 1 DECLARE @VAL VARCHAR(1000) DECLARE @SEPERATOR CHAR(1) = ',' DECLARE @SP INT DECLARE @WORK VARCHAR(MAX) = @csv WHILE PATINDEX('%' + @SEPERATOR + '%', @WORK) <> 0 BEGIN
SELECT
	@SP = PATINDEX('%' + @SEPERATOR + '%', @WORK)
SELECT
	@VAL = LEFT(@WORK, @SP - 1)
SELECT
	@WORK = STUFF(@WORK, 1, @SP, '')
INSERT INTO
	@r
VALUES
	(@I, @VAL)
SET
	@I = @I + 1
END IF @WORK <> ''
INSERT INTO
	@r
VALUES
	(@I, @WORK) RETURN
END
GO