如何将sql中的字符串拆分为多列
我有一张桌子 身份证件 名称 命名路径 1. 产品1 无效的 2. 产品2 产品1 3. 产品3 产品1 |产品2 4. 产品4 产品1 |产品2 |产品3如何将sql中的字符串拆分为多列,sql,Sql,我有一张桌子 身份证件 名称 命名路径 1. 产品1 无效的 2. 产品2 产品1 3. 产品3 产品1 |产品2 4. 产品4 产品1 |产品2 |产品3 这是我想到的最好的方法: select case when name_path is null then name when CHARINDEX ('|', name_path) = 0 then name_path else LEFT(name_path, CHARINDEX
这是我想到的最好的方法:
select
case
when name_path is null then name
when CHARINDEX ('|', name_path) = 0 then name_path
else LEFT(name_path, CHARINDEX ('|', name_path)-1) --CAST( CHARINDEX ('|', name_path) as nvarchar)
--else LEFT(name_path, CHARINDEX ('|', name_path))
--else SUBSTRING(name_path, LEN(LEFT(name_path, CHARINDEX ('|', name_path))) + 1, LEN(name_path) - LEN(LEFT(name_path,
-- CHARINDEX ('|', name_path))) - LEN(RIGHT(name_path, LEN(name_path) - CHARINDEX ('.', name_path))) - 1)
end as producttype,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 0 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))),'---','.')
end as product,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 1 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-1),'---','.')
end as productflavor1,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 2 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-2),'---','.') end as productflavor2,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 3 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-3),'---','.')
end as productflavor3,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 4 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-4),'---','.')
end as productflavor4,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 5 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-5),'---','.')
end as productflavor5,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 6 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-6),'---','.')
end as productflavor6,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 7 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-7),'---','.')
end as productflavor7,
case
when LEN(name_path) - LEN(REPLACE(name_path, '|', '')) = 8 then name
else REPLACE( PARSENAME(REPLACE(replace(name_path,'.','---') , '|' , '.'),LEN(name_path) - LEN(REPLACE(name_path, '|', ''))-8),'---','.')
end as productflavor8,
table.*
from table
哪个数据库管理系统?在SQL Server中,有一个
STRING\u SPLIT
我曾对此进行过研究,但未能使其发挥作用,以获得所需的结果。