gpt4 book ai didi

ssis - 将字符串转换为日期时间(使用 SSIS)

转载 作者:行者123 更新时间:2023-12-04 14:02:13 25 4
gpt4 key购买 nike

我想在 SQL Server 表的列(日期时间)中插入一个值“5/27/2013 16:42:37.490000”(从平面文件(DT_STR)中读取)。如果我尝试在派生列中使用 (DT_DBDATE) 或 DT_DBTIMESTAMP 进行转换,则会出现错误。

[Derived Column [130]] Error: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR.  The "component "Derived Column" (130)" failed because error code 0xC0049064 occurred, and the error row disposition on "output column "Derived Column 1" (155)" specifies failure on error. An error occurred on the specified object of the specified component.  There may be error messages posted before this with more information about the failure.

我该怎么办?

谢谢

最佳答案

该值为 datetime2 type .AFAIK SSIS 不支持 datetime2 。您需要将其作为字符串存储在数据库中,然后通过将其转换为 datetime2 来更新列。

这是Microsoft Connect Issue

更新:使用 DT_DBTIMESTAMP2您可以将字符串转换为正确的日期时间格式

下面的代码在派生列中工作得很好

(DT_DBTIMESTAMP2,7)"2013-5-27 16:42:37.490000"

7是这里的岁差。如果datetime的格式不同,上面的代码将不起作用。例如 MM/DD/YYYY HH:mm:ss.ffffff .

但是您可以使用 Script component并将不同日期时间格式的数组传递给 Datetime.ParseExact功能

Step1:拖拽一个Script组件,新建一个输出栏 DT_DBTIMESTAMP数据类型并将其命名为 NewDate .

Step2:编写下面的C#代码
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string[] format = new string[] { @"M/dd/yyyy HH:mm:ss.ffffff",
@"MM/dd/yyyy HH:mm:ss",
@"M/d/yyyy HH:mm:ss" ,
@"M/dd/yyyy HH:mm:ss.ffffff",
@"MM/dd/yyyy HH:mm:ss.ffffff",
@"M/d/yyyy HH:mm:ss.ffffff"};
DateTime dt = DateTime.ParseExact(Row.Date,
format ,
CultureInfo.InvariantCulture,
DateTimeStyles.None);

Row.newDate = dt;
}

关于ssis - 将字符串转换为日期时间(使用 SSIS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16813903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com