【问题标题】:Convert column to multiple rows without Split_Lines - SQL Server 2008在没有 Split_Lines 的情况下将列转换为多行 - SQL Server 2008
【发布时间】:2021-02-02 11:14:06
【问题描述】:

我想转换下面的SQL表:

User_Index   Emails
------------------------
  5          test@db.com;test1@db.com
  10         re2@db.com;re3@db.com

进入:

  User_Index   Emails
------------------------
  5          test@db.com
  5          test1@db.com
  10         re2@db.com
  10         re3@db.com

我使用的是 SQL Server 2008,所以 SPLIT_LINES 函数不起作用,我正在尝试用分号值拆分电子邮件列。

【问题讨论】:

    标签: sql sql-server-2008


    【解决方案1】:

    一种方法是递归 CTE:

    with cte as (
          select user_index, convert(varchar(max), null) as email, convert(varchar(max), emails + ';') as rest
          from t
          union all
          select user_index, left(rest, charindex(';', rest) - 1), stuff(rest, 1, charindex(';', rest) + 1, '')
          from cte
          where rest <> ''
         )
    select user_index, email
    from cte
    where email is not null;
    

    Here 是一个 dbfiddle。

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2013-12-05
      相关资源
      最近更新 更多