Reputation: 478
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.
Upvotes: 1
Views: 1066
Reputation: 24046
Try this:
declare @d varchar(20)='28JAN1952';
SELECT replace(CONVERT(date,RIGHT(@d,4)+SUBSTRING(@d,3,3)+LEFT(@d,2)),'-','')
Upvotes: 0
Reputation: 61211
The easiest route will be to add a Script Transformation.
Inside script task
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;
}
}
Upvotes: 2