1

I'm working in ETL process where I have a column in a table varchar with data as 28JUN1952. This will always be in this format i.e. 2 digits for date, 3 letters for months and 4 numbers for years.

I was able to convert this into a date column by I need this to come in yyyyMMdd format i.e. 28JUN1952 become 19520628. I was able split the years months and date, but unable to add a padding in the month if it's a 1 digit month for e.g. Jan or Feb.

Is there any other way to convert the data format or any other alternative to add a padding?

I need the conversion in SSIS only.

4

2 回答 2

2

最简单的方法是添加脚本转换。

  1. 在输入和输出选项卡上,展开输出 0,选择输出列,单击添加列。2. 将 Column 重命名为 ccyymmdd 并更改数据类型。我不确定你的目标类型是什么(int32,string?)但我会假设 string 因为我很懒
  2. 在 Input Columns 选项卡上,选择字符串日期源列(假设它称为 srcDate)
  3. 在脚本任务中,假设 C#,使用这样的代码

内部脚本任务

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    DateTime x = DateTime.MinDate;
    if (DateTime.TryParse(Row.srcDate, out x))
    {
        Row.ccyymmdd = x.ToString("yyyyMMdd");
    }
    else
    {
        // Handle NULL values however you wish
        // Assign "known bad value"
        // Row.ccyymmdd = "99991231";
        // Or set the null property to true (preferred)
        Row.ccyymmdd_IsNull = true;
    }

}
于 2012-07-24T07:01:40.083 回答
0

尝试这个:

declare @d varchar(20)='28JAN1952';

SELECT replace(CONVERT(date,RIGHT(@d,4)+SUBSTRING(@d,3,3)+LEFT(@d,2)),'-','')
于 2012-07-24T05:52:46.937 回答