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.列の名前をccyymmddのような名前に変更し、データ型を変更します。ターゲットタイプ(int32、string?)はわかりませんが、怠惰なので文字列と見なします
  2. [入力列]タブで、文字列の日付ソース列を選択します(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 に答える