如何修复“强制转换规范的无效字符值”在平面文件的日期列上?

发布于 2024-12-02 20:44:09 字数 853 浏览 1 评论 0 原文

我有一个 CSV 文件,其中以 {LF} 分隔每一行,还有一个日期列,日期格式为 "12/20/2010"(包括引号)

我的目标列是 SQL Server 2008 数据库日期类型表(不是日期时间)

在我的平面文件连接管理器中,我已将日期列配置为数据类型 date [DT_DATE],其中 TextQualified 设置为 true,并且柱子分隔符为 {LF} (它是每行的最后一列)。我将文本限定符设置为 "

当我尝试将其加载到 OLE 目标中时,出现以下错误

[TRN_DORPS [760]] 错误:SSIS 错误代码 DTS_E_OLEDBERROR。发生 OLE DB 错误。错误代码:0x80004005。 OLE DB 记录可用。来源:“Microsoft OLE DB Provider for SQL Server” Hresult:0x80004005 描述:“转换规范的字符值无效。”。 [TRN_DORPS [760]] 错误:输入“OLE DB 目标输入”(773) 上的输入列“CYCLE_DATE”(874) 存在错误。返回的列状态为:“由于可能会丢失数据,因此无法转换该值。”。

如果我附加数据查看器,管道中的值是 2010-12-20 00:00:00.0000000 - 这个时间组件是导致问题的原因吗?我尝试使用 (DT_DATE)(DT_DBDATE)[CYCLE_DATE] 删除时间组件,但无济于事,因为它在管道中保持不变

I have a CSV file with a {LF} delimiting each row and a date column with the date format as "12/20/2010" (including quotation marks)

My destination column is a SQL Server 2008 database table of type date (not datetime)

In my Flat File Connection Manager, I have configured the date column to be data type date [DT_DATE] with TextQualified set to true and the column delimiter as {LF} (it is the last column on each row). I have the text qualifier set to "

When I try to load this into an OLE Destination I get the following error

[TRN_DORPS [760]] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80004005 Description: "Invalid character value for cast specification.".
[TRN_DORPS [760]] Error: There was an error with input column ""CYCLE_DATE"" (874) on input "OLE DB Destination Input" (773). The column status returned was: "The value could not be converted because of a potential loss of data.".

If I attach a data viewer, the value in the pipeline is 2010-12-20 00:00:00.0000000 - is this time component what's causing the problem? I try to strip out the time component with (DT_DATE)(DT_DBDATE)[CYCLE_DATE] but to no avail as it stays the same in the pipeline

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔落墨 2024-12-09 20:44:10

我最终能够通过将平面文件连接中的列类型设置为“数据库日期 [DT_DBDATE]”类型来解决该解决方案

显然这些日期格式之间的差异如下:

DT_DATE
由年、月、日、和小时组成的日期结构。

DT_DBDATE
由年、月、日组成的日期结构。

DT_DBTIMESTAMP
由年、月、时、分、秒和分数组成的时间戳结构

通过将列类型更改为 DT_DBDATE,问题得到解决 - 我附加了一个数据查看器,CYCLE_DATE 值现在只是“12/20/2010”,没有时间组件,显然解决了这个问题。

I was ultimately able to resolve the solution by setting the column type in the flat file connection to be of type "database date [DT_DBDATE]"

Apparently the differences between these date formats are as follow:

DT_DATE
A date structure that consists of year, month, day, and hour.

DT_DBDATE
A date structure that consists of year, month, and day.

DT_DBTIMESTAMP
A timestamp structure that consists of year, month, hour, minute, second, and fraction

By changing the column type to DT_DBDATE the issue was resolved - I attached a Data Viewer and the CYCLE_DATE value was now simply "12/20/2010" without a time component, which apparently resolved the issue.

佞臣 2024-12-09 20:44:10

我的日期值错误,即我有一个针对 2022-09-31 运行的查询,这是不正确的,因为 9 月份不是 31 天的月份。将其更改为 2022-09-30 效果很好。

I had a wrong value for a date, i.e I had a query that was running against 2022-09-31 which is incorrect as the month of September is not a 31 days month. Changing it to 2022-09-30 worked fine.

謌踐踏愛綪 2024-12-09 20:44:09

为了模拟您面临的问题,我使用 SSIS 2008 R2SQL Server 2008 R2 后端创建了以下示例。该示例基于我从您的问题中收集到的内容。此示例未提供解决方案,但它可能会帮助您确定您的情况中问题可能出在哪里。

创建一个简单的 CSV 文件,其中包含两列,即订单号和订单日期。正如您在问题中提到的,两列的值都用双引号 (") 限定,行也以换行符 (\n) 结尾,日期是最后一列。下面的屏幕截图是使用 ,可以显示屏幕截图中的特殊字符 LF 表示换行。

="http://notepad-plus-plus.org/" rel="noreferrer" > src="https://i.sstatic.net/xz0yi.png" alt="Orders file">

在 SQL Server 数据库中创建了一个名为 dbo.Destination 的简单表来填充 CSV 文件数据下面给出了使用 SSIS 包创建表的脚本

CREATE TABLE [dbo].[Destination](
    [OrderNumber] [varchar](50) NULL,
    [OrderDate] [date] NULL
) ON [PRIMARY]
GO

,我使用 OLE DB 连接创建了两个连接管理器来连接到 SQL Server 数据库。平面文件 平面文件连接管理器

Connections

配置为读取 CSV 文件,设置如下所示。红色箭头表示所做的更改。

为平面文件连接管理器提供名称。浏览到 CSV 文件的位置并选择文件路径。输入双引号 (") 作为文本限定符。将标题行分隔符从 {CR}{LF} 更改为 {LF}。此标题行分隔符更改也反映了在“列”部分中。

平面文件常规

部分中未进行任何更改。

将列名称从 Column0 更改为 OrderNumber

src="https://i.sstatic.net/GkaC2.png" alt = src="https://i.sstatic.net/OqrZ0.png" alt="高级列 OrderNumber">

将列名称从 Column1 更改为 OrderDate 并且也更改这数据类型为日期 [DT_DATE]

高级列 OrderDate

平面文件连接中数据的预览 在

Data Preview

SSIS 包的 Control Flow 选项卡上,放置了 <代码>数据流任务。

Control Flow

在数据流任务中,放置了一个平面文件源和一个OLE DB 目标

Data Flow Task

平面文件源 配置为使用 FlatFile 连接读取 CSV 文件数据经理。下面的三个屏幕截图显示了平面文件源组件的配置方式。

平面文件源连接管理器

平面文件源列

平面文件源错误输出

已配置 OLE DB 目标 组件接受来自平面文件源的数据并将其插入到名为 dbo.Destination 的 SQL Server 数据库表中。下面的三个屏幕截图显示了 OLE DB 目标组件的配置方式。

OLE DB 目标连接管理器

OLE DB Destination Mappings

OLE DB Destination Error Output

使用以下 5 中提到的步骤屏幕截图中,我在平面文件源和 OLE DB 目标之间的流上添加了一个数据查看器。

右键单击

数据流路径编辑器新建

配置数据查看器

已添加数据流路径编辑器

数据查看器可见

在运行包之前,我验证了表中存在的初始数据。它目前是空的,因为我使用本文开头提供的脚本创建了它。

Empty Table

执行了包,包执行暂时暂停,以显示从平面文件源到 OLE DB 目标的数据流数据查看器。我单击运行按钮继续执行。

数据查看器暂停

包已成功执行。

Successfulexecution

平面文件源数据已成功插入到表 dbo.Destination 中。

Data in table

这是表 dbo.Destination 的布局。正如您所看到的,字段 OrderDate 的数据类型为 date,并且包仍然继续正确插入数据。

Destination layout

这篇文章虽然不是解决方案。希望可以帮助您找出您的场景中问题可能出在哪里。

In order to simulate the issue that you are facing, I created the following sample using SSIS 2008 R2 with SQL Server 2008 R2 backend. The example is based on what I gathered from your question. This example doesn't provide a solution but it might help you to identify where the problem could be in your case.

Created a simple CSV file with two columns namely order number and order date. As you had mentioned in your question, values of both the columns are qualified with double quotes (") and also the lines end with Line Feed (\n) with the date being the last column. The below screenshot was taken using Notepad++, which can display the special characters in a file. LF in the screenshot denotes Line Feed.

Orders file

Created a simple table named dbo.Destination in the SQL Server database to populate the CSV file data using SSIS package. Create script for the table is given below.

CREATE TABLE [dbo].[Destination](
    [OrderNumber] [varchar](50) NULL,
    [OrderDate] [date] NULL
) ON [PRIMARY]
GO

On the SSIS package, I created two connection managers. SQLServer was created using the OLE DB Connection to connect to the SQL Server database. FlatFile is a flat file connection manager.

Connections

Flat file connection manager was configured to read the CSV file and the settings are shown below. The red arrows indicate the changes made.

Provided a name to the flat file connection manager. Browsed to the location of the CSV file and selected the file path. Entered the double quote (") as the text qualifier. Changed the Header row delimiter from {CR}{LF} to {LF}. This header row delimiter change also reflects on the Columns section.

Flat File General

No changes were made in the Columns section.

Flat File Columns

Changed the column name from Column0 to OrderNumber.

Advanced column OrderNumber

Changed the column name from Column1 to OrderDate and also changed the data type to date [DT_DATE]

Advanced column OrderDate

Preview of the data within the flat file connection manager looks good.

Data Preview

On the Control Flow tab of the SSIS package, placed a Data Flow Task.

Control Flow

Within the Data Flow Task, placed a Flat File Source and an OLE DB Destination.

Data Flow Task

The Flat File Source was configured to read the CSV file data using the FlatFile connection manager. Below three screenshots show how the flat file source component was configured.

Flat File Source Connection Manager

Flat File Source Columns

Flat File Source Error Output

The OLE DB Destination component was configured to accept the data from Flat File Source and insert it into SQL Server database table named dbo.Destination. Below three screenshots show how the OLE DB Destination component was configured.

OLE DB Destination Connection Manager

OLE DB Destination Mappings

OLE DB Destination Error Output

Using the steps mentioned in the below 5 screenshots, I added a data viewer on the flow between the Flat File Source and OLE DB Destination.

Right click

Data Flow Path Editor New

Configure Data Viewer

Data Flow Path Editor Added

Data Viewer visible

Before running the package, I verified the initial data present in the table. It is currently empty because I created this using the script provided at the beginning of this post.

Empty Table

Executed the package and the package execution temporarily paused to display the data flowing from Flat File Source to OLE DB Destination in the data viewer. I clicked on the run button to proceed with the execution.

Data Viewer Pause

The package executed successfully.

Successful execution

Flat file source data was inserted successfully into the table dbo.Destination.

Data in table

Here is the layout of the table dbo.Destination. As you can see, the field OrderDate is of data type date and the package still continued to insert the data correctly.

Destination layout

This post even though is not a solution. Hopefully helps you to find out where the problem could be in your scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文