Friday, 18 March 2016

Get CSV Values in SQL

Query:

DECLARE @CSVValue NVARCHAR(50)='100,101,102'                  
DECLARE @eachValue NUMERIC(9,0)
 
while len(@CSVValue) > 0
BEGIN
SET @eachValue=left(@CSVValue, charindex(N',', @CSVValue+N',')-1)
  PRINT CONVERT(VARCHAR(20),@eachValue)            
SET @CSVValue = stuff(@CSVValue, 1, charindex(N',', @CSVValue+N','), N'')
END


Output:

100
101
102



No comments:

Post a Comment

Get CSV Values in SQL

Query: DECLARE @CSVValue NVARCHAR(50)='100,101,102'                   DECLARE @eachValue NUMERIC(9,0)   while len(@CSVValue) ...