23

i have a table with 3 columns (First_ID,Second_ID,Third_ID) all columns are int columns.

Now I have 3 values, first and third values are int values (1 and 0), the second value is a comma delimited string ('188,189,190,191,192,193,194')

what should be my approach to populate the table like the bellow:

1   188 0
1   189 0
1   190 0
1   191 0
1   192 0
1   193 0
1   194 0

I have tried different ways but could not get it to work as i want.

Thanks in advance

DineshDB
  • 5,998
  • 7
  • 33
  • 49
user3430556
  • 251
  • 1
  • 2
  • 5

4 Answers4

24

Better use XML for this,

Declare @Var nvarchar(MAX)

Set @Var ='188,189,190,191,192,193,194'

DECLARE @XML AS XML

DECLARE @Delimiter AS CHAR(1) =','

SET @XML = CAST(('<X>'+REPLACE(@Var,@Delimiter ,'</X><X>')+'</X>') AS XML)

DECLARE @temp TABLE (ID INT)

INSERT INTO @temp

SELECT N.value('.', 'INT') AS ID FROM @XML.nodes('X') AS T(N)

SELECT * FROM @temp
bummi
  • 27,123
  • 14
  • 62
  • 101
Jom George
  • 1,107
  • 10
  • 21
16

Using the Split() function you have mentioned in comments,

-- Variable holding comma separated values
DECLARE @Var VARCHAR(4000);
SET @Var =  '188,189,190,191,192,193,194'

-- Test Target Table
DECLARE @Target_Table TABLE  (First_ID INT,Second_ID INT,Third_ID INT) 

-- Insert statement
INSERT INTO @Target_Table
SELECT 1, CAST(Items AS INT) , 0 
FROM  dbo.Split(@Var, ',')  

-- Test Select
SELECT * FROM  @Target_Table  

Result Set

╔══════════╦═══════════╦══════════╗
║ First_ID ║ Second_ID ║ Third_ID ║
╠══════════╬═══════════╬══════════╣
║        1 ║       188 ║        0 ║
║        1 ║       189 ║        0 ║
║        1 ║       190 ║        0 ║
║        1 ║       191 ║        0 ║
║        1 ║       192 ║        0 ║
║        1 ║       193 ║        0 ║
║        1 ║       194 ║        0 ║
╚══════════╩═══════════╩══════════╝  
Community
  • 1
  • 1
M.Ali
  • 67,945
  • 13
  • 101
  • 127
  • 7
    Split is not a function in SQL Server. Using SQL Server 2012. Do i need to do any special thing – Avinash Jain Aug 31 '16 at 12:11
  • 6
    There is no SQL Server function split() – defines Nov 10 '16 at 14:44
  • To the people commenting (correctly) that SQL Server doesn't contain a Split() function; this answer is suggesting OP create a dbo.split function as seen in the question being linked to. This answer is showing OP how to use the split function created in that other question to handle their slightly more complicated situation. – Keiki Oct 12 '17 at 19:28
  • For SQL Server 2012 or above,can use "dbo.SplitString()". works similar to split() function. :) – siddaramesh Dec 17 '18 at 14:04
6

From SQL server 2016 onwards you can use this function string_split

DECLARE @Var VARCHAR(4000);
SET @Var =  '188,189,190,191,192,193,194'

SELECT 1 as First_ID, value as Second_ID ,0 as Third_ID FROM string_split(@Var,',')
Rolwin Crasta
  • 4,219
  • 3
  • 35
  • 45
1
DECLARE @Table VARCHAR(100)='AAA,BBB,CCC,DDD'
IF OBJECT_ID('[Comma_Split]') IS NOT NULL
DROP TABLE [Comma_Split]
CREATE TABLE [Comma_Split](ID INT IDENTITY(1,1),COL VARCHAR(100))
while LEN(@Table)>0
BEGIN
DECLARE @COMMA INT= CHARINDEX(',', @Table)
IF @COMMA=0 SET @COMMA=LEN(@Table)+1
INSERT INTO [Comma_Split]
SELECT SUBSTRING(@Table,1,@COMMA-1)
SET @COMMA=@COMMA+1
SET @Table=SUBSTRING(@Table,@COMMA,LEN(@Table))
END