I receive an excel file with time fields in the format :00:00, but there are times when the hour will be populated
| Time | Seconds
| ——–
| 1:03:48 |
| :52:10 |
I’m having a bit of trouble converting to seconds. I’ve tried using TOKEN function, but not recognizing the hour segment
The results should be
Time | Seconds |
---|---|
1:03:48 | 3828 |
:52:10 | 3130 |
The easiest way to work with strings in SSIS is via a Script Component.
- Add a script component transformation
- For input columns, select the column with the time string. In my example I used
time
- For inputs and outputs, add a column,
Seconds
and set the datatype to double: - On the script tab, hit the edit script button and modify the method,
Input0_ProcessInputRow()
:
public override void Input0_ProcessInputRow(Input0Buffer Row) { string current_time = Row.time.Trim(); try { if (current_time.StartsWith(":")) { current_time = "0" + current_time; } Row.Seconds = TimeSpan.Parse(current_time).TotalSeconds; } catch (Exception e) { ComponentMetaData.FireError(10, "Convert to Seconds", e.Message, "", 0, out bool cancel); } }
Output will look like this:
2