简体   繁体   English

如何在sql server中从csv格式的文本文件中获取表格的数据

[英]How to get data of a table filled from a text file with csv format in sql server

This is the data I get from the bulk import and I have in a table called Drinks:这是我从批量导入中获得的数据,我在名为 Drinks 的表中有:

| Descrip       |  10/02/20 07   |  17/02/20 08  |  24/02/20 09 |
| SL AGUA       |     NULL       |     NULL      |     2.861    |
| ALHAMBRA IPA  |     350        |     NULL      |     NULL     |
| Carlsberg     |     800        |     2.800     |     2.800    |
| CASIMIRO MH   |     NULL       |      55       |     NULL     |

I need to do a query to obtain the data like this:我需要做一个查询来获取这样的数据:

| SL AGUA       |      20      |    09    |   2861   |
| ALHAMBRA IPA  |      20      |    07    |    350   |
| Carlsberg     |      20      |    07    |    800   |
| Carlsberg     |      20      |    08    |   2800   |
| Carlsberg     |      20      |    09    |   2800   |
| CASIMIRO MH   |      20      |    08    |     55   |

The first row has data like header (Descrip) but has also data I need like date and number of the week.第一行有像标题(描述)这样的数据,但也有我需要的数据,比如日期和星期几。

Is it possible to do what I need?可以做我需要的吗? Thanks.谢谢。

Load the data into a staging table, with columns from the first row.将数据加载到临时表中,列来自第一行。

Then unpivot, I would recommend using apply :然后unpivot,我建议使用apply

select s.descrip, v.col1, vol2, v.val
from staging s cross apply
     (values ([10/02/20 07], 20, 7),
             ([10/02/20 08], 20, 8),
             ([10/02/20 09], 20, 9)
     ) v(val, col1, col2)
where v.val is not null;  -- or perhaps v.val <> ''

Follow the below step:请按照以下步骤操作:

  1. Table creation & insert Data表创建和插入数据

create table Drinks
(
    Head1 nvarchar(100),
    Head2 nvarchar(100),
    Head3 nvarchar(100),
    Head4 nvarchar(100)
)

insert into Drinks values('Descrip','10/02/20 07','17/02/20 08','24/02/20 09'),
                    ('SL AGUA',NULL,NULL,'2.861'),
                    ('ALHAMBRA IPA','350',NULL, NULL),
                    ('Carlsberg','800','2.800','2.800'),
                    ('CASIMIRO MH',NULL,'55',NULL)
  1. Result Query结果查询

;with tempCTE
as (
select head1,head2,head3,head4,
head5=(case when head2 is not null then head5 else '' end) ,
head6=(case when head3 is not null then head6 else '' end) ,
head7=(case when head4 is not null then head7 else '' end) 
from Drinks
cross apply (
    select  
    head5=right(Head2,2) + '|' + substring(Head2,7,2),
    head6=right(Head3,2)+'|'+substring(Head3,7,2),
    head7=right(Head4,2)+'|'+substring(Head4,7,2) 
     from Drinks
    where head1='Descrip'
) as tbl
cross apply (
        select 1 slno union select 2
) as tbl2
where head1<>'Descrip'

), resultCTE
 as
(
    select distinct head1,SplitHead1=left(head5,2),SplitHead2=right(head5,2),head2 from tempCTE where head2 is not null
    union
    select distinct head1,left(head6,2) ,right(head6,2),head3 from tempCTE where head3 is not null
    union
    select distinct head1,left(head7,2),right(head7,2),head4 from tempCTE where head4 is not null
)
select * from resultCTE

输出

Fiddle DB Output 小提琴数据库输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取从SQL Server 2008导出的CSV表格或文本文件的数据类型 - How to get the data type of table of csv or text file that is exported from sql server 2008 如何获取从SQL存储过程生成的CSV文件可以具有文本格式? - How to get the CSV file generated from SQL store procedure can have text format? 是否从SQL以CSV文件格式导出数据? - Exporting data in CSV file format from SQL? CSV 文件中的错误日期格式转换为 SQL 服务器表与 Python - Incorrect date format in CSV file converting to SQL Server Table with Python 以CSV格式导出数据表SQL Server 2008 - Export a data table in CSV format sql server 2008 如何将SQL Server表数据作为插入命令导出到文本文件? - How to export SQL Server table data into text file as Insert command? 如何使用“ bcp”实用程序将数据从表传输到SQL Server 2000中的文本文件 - how to use 'bcp' utility to transfer data from table to text file in sql server 2000 如何在SQL Server中插入充满ParentCode的表 - How to insert table filled with parentCode in SQL Server 从CSV导入到SQL Server时如何将日期列的文本数据类型转换为时间戳格式 - How to convert text datatype of date column to timestamp format when importing from CSV to SQL server 如何从SQL Server表中导出文本数据? - How to export text data from a SQL Server table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM