使用 SQL 服务器导入向导将 CSV 上传到 table 时出现日期错误

Date Error when Uploading CSV to table using SQL Server Import Wizard

我在 SQL 服务器的数据库中创建了一个 table,现在正尝试通过导入向导将 CSV 导入到同一个 table 中,我得到以下信息错误:

Error 0xc0202009: Data Flow Task 1: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 11.0"  Hresult: 0x80004005  Description: "Invalid date format".
 (SQL Server Import and Export Wizard)

我尝试上传的 CSV 格式与我用来创建 table 的格式完全相同,但问题似乎与日期有关:

The column status returned was: "Conversion failed because the data value overflowed the specified type.". (SQL Server Import and Export Wizard)

原始 CSV 日期列的格式为:

EVENT_DT
31/12/2016 1:40
31/12/2016 2:55
31/12/2016 3:30
31/12/2016 3:30
31/12/2016 3:30

我尝试上传的 CSV 格式如下:

EVENT_DT
2/04/2020 6:55
2/04/2020 3:50
2/04/2020 4:25
2/04/2020 5:45
2/04/2020 3:15
2/04/2020 3:15

我尝试在 Import Qizard 中将 EVENT_DT 数据类型更改为 database timestamp with precision [DT_DBTIMESTAMP2],但仍然出现错误。

谁能指出我做错了什么?

请尝试将平面文件数据导入暂存 table(临时 table),所有数据类型均为 varchar。

完成后,您可以根据需要将其转换为日期时间,同时将数据传输到主table。

以下是在将数据插入主 table 时将字符串转换为日期时间的方法 table。

DECLARE @temp1 TABLE (ID INT, EVENT_DT VARCHAR(20))

INSERT INTO @temp1 VALUES (1, '31/12/2016 1:40')
INSERT INTO @temp1 VALUES (2, '31/12/2016 2:55')
INSERT INTO @temp1 VALUES (3, '31/12/2016 3:30')
INSERT INTO @temp1 VALUES (4, '31/12/2016 3:30')
INSERT INTO @temp1 VALUES (5, '31/12/2016 3:30')

DECLARE @temp2 TABLE (ID INT, EVENT_DT DATETIME)

INSERT INTO @temp2
SELECT ID, convert(DATETIME, EVENT_DT, 103) FROM @temp1

SELECT * FROM @temp2