在没有 Split_Lines 的情况下将列转换为多行 - SQL Server 2008
Convert column to multiple rows without Split_Lines - SQL Server 2008
我想转换以下 SQL table:
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
功能不起作用,我正在尝试按分号值拆分电子邮件列。
一种方法是递归 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 是一个 db<>fiddle.
我想转换以下 SQL table:
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
功能不起作用,我正在尝试按分号值拆分电子邮件列。
一种方法是递归 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 是一个 db<>fiddle.