【问题标题】:separate values by new line and split to new row [duplicate]按新行分隔值并拆分到新行[重复]
【发布时间】:2021-03-23 11:16:15
【问题描述】:

如何在 SQL Server 中按新行分隔值并拆分到新行。 我有以下数据

Ticket Agent
98765 Agent1 Agent2 Agent3
12345 Agent4 Agent5 Agent6

或者如下

现在我必须将其转换为以下 -

Ticket Agent
98765 Agent1
98765 Agent2
98765 Agent3
12345 Agent4
12345 Agent5
12345 Agent6

【问题讨论】:

  • 你有没有尝试过?拆分值之前已经被问过很多次了

标签: sql sql-server


【解决方案1】:

使用string_split():

select t.ticket, s.value as agent
from t cross apply
     string_split(t.agent, '
') s
order by s.value;

Here 是一个 dbfiddle。

【讨论】:

    猜你喜欢
    • 2015-03-07
    • 2019-07-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多