我导入的平面文件已使用逗号分隔符导入到SQL中。 我的文本文件的示例如下:
Location\Floor\Room,Date,Value
导入后:
Column 1 | Column 2 | Column 3
Location\Floor\Room | Date | Value
我希望我的桌子看起来如下:
Column 1 | Column 2 | Column 3 | Column 4 | Column 5
Location | Floor | Room | Date | Value
我有什么办法可以像上面那样实现吗?
答案 0 :(得分:0)
我正在考虑这种解决方案。
select t2.col1
, t2.col2
, substring(t2.col3, charindex('\', t2.col3, len(t2.col2) + len(t2.col1)) + 1, len(t2.col3) - (len(t2.col2) + len(t2.col1) + 2))
, t2.[value], t2.[date]
from (
select t1.col1, substring(t1.main, len(t1.col1) + 2
, charindex('\', t1.main, len(t1.col1) + 2) - (len(t1.col1) + 2)) as col2
, t1.main as col3, t1.[value], t1.[date]
from (
select substring(column1, 0, charindex('\', column1)) as col1, column1 as main, [date], [value]
from tableA
) t1
) t2
答案 1 :(得分:0)
这适用于未分割字符串的MAX 5值
val1 \ val2 \ val3 \ val4 \ val5
select [1] as col1, [2] as col2, [3] as col3, [4] as col4, [5] as col5, col2 as col7, col3 as col8
from (
select ROW_NUMBER() over(partition by col1 order by col1) rowid, col1, col2, col3, value
from <MyTable>
cross apply string_split(s.col1, '\')
) as tbl
pivot (
max(value) for rowid in ([1], [2], [3], [4], [5])
) as pv
答案 2 :(得分:0)
SSIS - SQL Server Integration Service
也可以用于此用例。
您基本上需要一个两步转换过程,在此过程中,您将输入文件加载到临时表中,该表允许将comma
作为标准定界符。
一旦您有了可用的临时表,并且有了记录(包括带有backslash
的记录),则应在SSIS中使用Derived Column Task
并基于SUBSTRING() and FINDSTRING()
方法创建自定义逻辑创建新列以根据backslash