我在我的表中有一个名为course的列,其行为lyk
row1 : course:1|course:2|course:3
row2 : Course:2|course:4|NUll
现在我必须消除过滤器'|'在行中并将不同的值存储在另一个表中..我怎么做... 使用SP
我将输出列除外:
Course:1
Course:2
Course:3
Course:4
提前致谢
答案 0 :(得分:0)
试试这个
create table table1(A varchar(50),B varchar(50),C varchar(50));
insert into table1 values('course:1','course:2','course:3');
insert into table1 values('course:2','course:4',null);
SQL查询
Select Distinct value
From table1
unpivot
(
value
For Cource in (A, B, C)
) U;
<强> Fiddle Demo 强>
<强>输出:强>
+-----------+
| VALUE |
+-----------+
| course:1 |
| course:2 |
| course:3 |
| course:4 |
+-----------+
答案 1 :(得分:0)
尝试使用此功能拆分行值,请查看此link
然后,如果你的数据是这样的:
create table table1 (course varchar(1000))
insert into table1 ( course) values ('course:1|course:2|course:3'),('course:2|course:4|NULL')
此查询使用函数来拆分字符串,您可以将此代码放入存储过程
select ROW_NUMBER()over(order by course)as Number,*
into #temp
from table1
declare @flag int, @count int
select @count=COUNT(''), @flag=1 from table1
create table #result(items varchar(100))
while @flag<=@count
begin
declare @row varchar(1000)
select @row=course from #temp where Number=@flag
insert into #result
select Items from [dbo].[Split](@row,'|')
where Items<>'NULL'
set @flag=@flag+1
end
select distinct * from #result
drop table #result
drop table #temp
输出:
+---------+
|Items |
+---------+
|course:1 |
|course:2 |
|course:3 |
|course:4 |
+---------+