sqlserver 3a can we select from comma seperated string variable value

Solutions on MaxInterview for sqlserver 3a can we select from comma seperated string variable value by the best coders in the world

showing results for - "sqlserver 3a can we select from comma seperated string variable value"
Maximilian
20 May 2018
1/****** Object:  UserDefinedFunction [dbo].[CSVToTable]    Script Date: 04/28/2013 10:45:17 ******/
2SET ANSI_NULLS ON
3GO
4
5SET QUOTED_IDENTIFIER ON
6GO
7
8CREATE FUNCTION [dbo].[CSVToTable] (@InStr VARCHAR(MAX))
9RETURNS @TempTab TABLE
10   (id int not null)
11AS
12BEGIN
13    ;-- Ensure input ends with comma
14	SET @InStr = REPLACE(@InStr + ',', ',,', ',')
15	DECLARE @SP INT
16DECLARE @VALUE VARCHAR(1000)
17WHILE PATINDEX('%,%', @INSTR ) <> 0 
18BEGIN
19   SELECT  @SP = PATINDEX('%,%',@INSTR)
20   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
21   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
22   INSERT INTO @TempTab(id) VALUES (@VALUE)
23END
24	RETURN
25END
26GO