Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Split two data into table

$
0
0

in stored procedure have query like this :

declare@col1_list varchar(max),@col2_list varchar(max)declare@tbl TABLE(col1 int , col2 int)set@col1_list ='2|6|7|4|3|'set@col2_list ='1|'

want split data into table , it should be like

col1  col22161714131

maybe col1 have one value in list and many values in col2 for example

set@col1_list ='2|'set@col2_list ='1|2|3|4'

it should be like :

col1  col221222324

How to do it ?

I split one list like this , but cant put another list split

declare @col1_list varchar(max)
DECLARE @pos INT = 0
DECLARE @len INT = 0
DECLARE @value varchar(8000)

set @col1_list = '2|6|7|4|3|'

WHILE CHARINDEX('|', @col1_list, @pos+1)>0
BEGIN
set @len = CHARINDEX('|', @col1_list, @pos+1) - @pos
set @value = SUBSTRING(@col1_list, @pos, @len)
insert into @tbl(col1) values(@value)
set @pos = CHARINDEX('|', @col1_list, @pos+@len) +1
end

select * from @tbl

Dont need function , only want to do in query . Thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>