Question

I see similar questions, but most have to do with datetime or fairly different requirements.

I have a CSV report that has a time column stored as HH:MM[:SS] (if over 1 day, it adds :00 for seconds... annoyingly, and I don't have control over how the report itself runs.) I think, in the derived column task, we can drop seconds.

Examples:

  • 23:54 = 23H 54M
  • 27:35:00 = 27H 35M 00S

My questions are:

  1. If the CSV loads the column as text, what expression do I need for the derived column?
  2. What should I set the data type as in the SQL database?

Pertinent info: SQL Server 2014 and SSDT for VS 2013

Thank you, Wes

Was it helpful?

Solution

2) Time/Date datatypes are often problematic choices for storing durations. Instead use an integer or decimal for whatever resolution is appropriate (seconds or milliseconds are popular, but minutes may work for you in this case).

1) In this instance, the simplest option may be: (DT_I4)TOKEN(Column,":",1) * 60 + (DT_I4)TOKEN(Column,":",2).

Other methods:

  • If you will always have two characters each for hours and minutes, you can use something like (DT_I4)SUBSTRING( [Column], 1, 2 ) * 60 + (DT_I4)SUBSTRING( [Column], 4, 2 ).
  • If hours can be 1 or more characters, but minutes is always two: (DT_I4)SUBSTRING(Column,1,FINDSTRING(Column,":",1) - 1) * 60 + (DT_I4)SUBSTRING(Column,FINDSTRING(Column,":",1) + 1,2)
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top