SELECT Col1, value AS Col2 INTO Table_2
FROM your_table_name
CROSS APPLY STRING_SPLIT(Col2, '/');
SELECT Col2, value AS Col1 INTO Table_3
FROM Table_2
CROSS APPLY STRING_SPLIT(Col1, '/');
SELECT * FROM Table_3;
SELECT (left bit of column 1), (left bit of column2)
UNION ALL
SELECT (middle bit of column 1), (middle bit of column 2)
where [column 1] like '%/%'
UNION ALL
SELECT (last bit of column 1), (last bit of column 2)
where [column 1] like '%/%/%'
select t.*, ss.*
from t cross apply
(select s1.value as value1, s2.value as value2
from (select s1.value,
row_number() over (order by charindex('/' + s1.value + '/', '/' + t.col1 + '/')) as pos
from string_split(t.col1, '/') s1
) s1 join
(select s2.value,
row_number() over (order by charindex('/' + s2.value + '/', '/' + t.col2 + '/')) as pos
from string_split(t.col2, '/') s2
) s2
on s1.pos = s2.pos
) ss;
4条答案
按热度按时间sirbozc51#
你试过了吗
CROSS APPLY?
请用表名替换“your\u table\u name”。应该可以,只要复制粘贴就行了。rn0zuynd2#
不容易,但可行。
我会“压平”table:
如果您有更多的斜杠和数据的可能性,您需要添加更多的联合。
使用charindex查找斜线和子字符串以提取位。
eqfvzcg83#
或许可以帮上忙?https://docs.microsoft.com/it-it/sql/t-sql/functions/string-split-transact-sql?view=sql-server-ver15 看看例子d和e
eqfvzcg84#
不幸的是,SQLServer中内置的字符串拆分函数不返回字符串中的位置。在我看来,这是一个重大的疏忽。
假设字符串没有重复的值,可以使用
row_number()
以及charindex()
要添加枚举:这是一把小提琴。