字符串日期到 YYYY-MM-DD 字符串格式

String date to YYYY-MM-DD string format

我正在使用 SSIS 2013,我正在使用字符串变量来存储从文件创建的日期...

我的问题是我正在获取字符串值 Jan 25 2018 12:00:00,当我将数据加载到数据库时,该列需要字符串值 2018-01-25。如何转换派生列任务中的变量?

使用派生列

据此Microsoft article:

When a string is cast to a DT_DATE, or vice versa, the locale of the transformation is used. However, the date is in the ISO format of YYYY-MM-DD, regardless of whether the locale preference uses the ISO format.

因此您可以在派生列中简单地使用以下表达式:

SUBSTRING( (DT_STR,50, 1256)(DT_DATE)[DateColumn], 1, 10)

使用脚本组件

假设inDate是输入列,OutDate是输出列

using System;
using System.Globalization;


CultureInfo provider = CultureInfo.InvariantCulture;

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    Row.outDate = DateTime.ParseExact(Row.inDate,"MMM dd yyyy HH:mm:ss",provider).ToString("yyyy-MM-dd");
}