How do I fix 'Invalid character value for cast specification' on a date column in flat file?

后端 未结 3 401
春和景丽
春和景丽 2021-01-04 04:48

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 co

相关标签:
3条回答
  • 2021-01-04 05:05

    The proper data type for "2010-12-20 00:00:00.0000000" value is DATETIME2(7) / DT_DBTIME2 ().

    But used data type for CYCLE_DATE field is DATETIME - DT_DATE. This means milliseconds precision with accuracy down to every third millisecond (yyyy-mm-ddThh:mi:ss.mmL where L can be 0,3 or 7).

    The solution is to change CYCLE_DATE date type to DATETIME2 - DT_DBTIME2.

    0 讨论(0)
  • 2021-01-04 05:11

    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.

    0 讨论(0)
  • 2021-01-04 05:28

    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.

    0 讨论(0)
提交回复
热议问题