I have an input string
100|2|3,101|2|1,103|2|3.
I would like to parse and add this in table having 3 columns so it should go f.x
col1 col2 col3
100 2 3
similar other data separated by comma as record and | as column.
Thanks
nik
I have an input string
100|2|3,101|2|1,103|2|3.
I would like to parse and add this in table having 3 columns so it should go f.x
col1 col2 col3
100 2 3
similar other data separated by comma as record and | as column.
Thanks
nik
Try this way
DECLARE @TAB TABLE(COLUMN1 INT, COLUMN2 INT, COLUMN3 INT)
DECLARE @STRING VARCHAR(MAX)='100|2|3,101|2|1,103|2|3,'
SELECT @STRING = 'SELECT ' + REPLACE( REPLACE (@STRING, ',','
UNION ALL
SELECT '),'|',',')
SELECT @STRING = SUBSTRING(@STRING,1,LEN(@STRING)-18)
INSERT INTO @TAB
EXEC(@STRING)
SELECT * FROM @TAB
And the result will be
+---------+---------+---------+
| COLUMN1 | COLUMN2 | COLUMN3 |
+---------+---------+---------+
| 100 | 2 | 3 |
| 101 | 2 | 1 |
| 103 | 2 | 3 |
+---------+---------+---------+
Declare @temp table(col1 int,col2 int ,col3 int)
Declare @pos int,@str nvarchar(max),@len int
Set @str='100|2|3'
Set @pos=1
Select @len=len(@str)
Insert into @temp
Select substring(@str,@pos,charindex('|',@str,@pos)-1),
substring(@str,charindex('|',@str,@pos)+1,charindex('|',@str,@pos)-3),
substring(@str,charindex('|',@str,charindex('|',@str,@pos)+1)+1,@len)
Select * from @temp