calculate time difference between two dates in ssis - sql

I have the following two columns:
StartDate = 2017-01-01 00:00:00.000
EndDate = 2017-01-01 05:45:00.000
I need to write an SSIS expression for my derived column that will calculate the time between these two datetimes. Output should be:
05:45:00.0000000
Can anyone help with writing this expression?
Thanks in advance!!

You can use DATEDIFF() function to get the difference between two dates.
difference in Hours
DATEDIFF("Hh",[StartDate],[EndDate])
difference in minutes
DATEDIFF("mi",[StartDate],[EndDate])
difference in minutes
DATEDIFF("ss",[StartDate],[EndDate])
Suggested Expression to return HH:mm:ss
You have to get the difference in seconds then use the following expression
RIGHT("000" + (DT_WSTR,3)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) / 3600),3) + ":" + RIGHT("00" + (DT_WSTR,2)((DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) % 3600) / 60) ,2) + ":" + RIGHT("00" + (DT_WSTR,2)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate])% 60),2)
References
DATEDIFF (SSIS Expression)
Convert Seconds to HH:MM:SS using SSIS
SSIS Expression – Convert Seconds to HHH:MM:SS Format

There is no direct function that gives you the expected output, you have to get the difference between both dates in the minimal unit you want (seconds or milliseconds) then you should build your own expression that convert it to HH:mm:ss format)
You can use the following expression to get the difference between two dates:
RIGHT("00" + (DT_WSTR,10)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) / 3600),2) + ":" +
RIGHT("00" + (DT_WSTR,10)((DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) % 3600) / 60) ,2) + ":" +
RIGHT("00" + (DT_WSTR,10)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate])% 60),2)

Related

Challenge to change source date formats to desired date formats

Someone, please assist me in solving this challenge:
I am using Zappsys Json Source on my dataflow and trying to transfer date to the OLEDB destination.
Googlespreadsheet Source: ActualClosingDate : 7/29/16 as m/dd/yy format
I want to convert it to OLEDB destination as
2016-07-29 15:00:00:000 format using derived column expression.
Thank you
Take a drived column component and convert the date as expected like following example:
(DT_STR,4,1252)DATEPART("yyyy",GETDATE()) + RIGHT("0" + "-" + (DT_STR,2,1252)DATEPART("mm",GETDATE()),2) + "-" + RIGHT("0" + (DT_STR,2,1252)DATEPART("dd",GETDATE()),2) + "-" + " " + (DT_STR,2,1252)DATEPART("hour", GETDATE()) + ":" + (DT_STR,2,1252)DATEPART("minute", GETDATE())+ ":" + (DT_STR,2,1252)DATEPART("second", GETDATE()) + ":" + (DT_STR,2,1252)DATEPART("millisecond", GETDATE())
To convert this value to a DateTime value, you can simply use a cast operator in a derived column as follows:
(DT_DBTIMESTAMP)[ActualClosingDate]
If you are looking to store this column as a string with a specific format (since DateTime columns have no format) just reconvert it to a string, and it will automatically ISO formatted:
(DT_WSTR,50)(DT_DBTIMESTAMP)[ActualClosingDate]

Data not getting fetched from Source using ODBC Connection and passing the SQL Query from a variable

I have a source query, wherein I am fetching data from Horton using ODBC connection
Select * from Table1 Where CreationDate > '2020-09-24 00:00:001'
When I run this query manually it runs fine, but when I run my SSIS Package no data is being fetched.
Please note I am passing this SQL Query from a variable and all variables are correctly passed. I have checked it using Edit Breakpoints.
This are the steps I followed to pass the query from a variable
The issue with your query appears to be the default formatting for translating an SSIS DateTime type to a string representation. The ODBC source appears to need a YYYY-MM-DD hh:mm:ss:mss (where mss is milliseconds but not the correct format code).
I tend to break my long expression into multiple variables and then have a "simple" final form that puts them all together.
Date_YYYYMMDD -> (DT_WSTR, 4) YEAR(#[System::ContainerStartTime]) + "-" + RIGHT("0" + (DT_WSTR, 2) MONTH(#[System::ContainerStartTime]),2) + "-" + RIGHT("0" + (DT_WSTR, 2) DAY(#[System::ContainerStartTime]),2)
That builds my YYYY-MM-DD format string. Data type is String
Date_HHMMSSms -> RIGHT("0" + (DT_WSTR, 2) DATEPART( "Hour", #[System::ContainerStartTime] ),2) + ":" + RIGHT("0" + (DT_WSTR, 2) DATEPART( "Minute", #[System::ContainerStartTime] ),2) + ":" + RIGHT("0" + (DT_WSTR, 2) DATEPART( "Second", #[System::ContainerStartTime] ),2) + ":" + RIGHT("0" + (DT_WSTR, 3) DATEPART( "Millisecond", getdate() ),3)
This builds out the time component. I always advocate for using the System variable ContainerStartTime instead of GETDATE() as getdate is evaluated every time we access it while container start time is constant for the execution of a package. What I discovered and can't wait to blog about is the the expression language doesn't appear to get the milliseconds out of a ContainerStartTime but does work correctly for GETDATE() calls.
The final date variable then becomes something like
Date_Filter -> #[User::Date_YYYYMMDD] + " " + #[User::Date_HHMMSSms]
Which then makes my query usage look like
Query_Source -> "Select * from Table1 Where CreationDate > '" + #[User::Date_Filter] + "'"

PROGRESS ODBC SQL Covert string into time

i need to convert string into time. I have 2 columns, one of them have date in good format but time looks like this:
Time
_____
061923
060239
134803
135011
first 2 characters are hours, second 2 are minutes and last 2 are seconds. How to convert it into format like "HH:MM:SS" ? Im using progress odbc.
You can insert ':' into the string and convert to a time:
select cast( (left(col, 2) + ':' + substring(col, 3, 2) + ':' + right(col, 2)) as time)

Changing Date format in SSIS expression

In SSIS, I have an expression that goes:
"SELECT _sessions.session_id AS session_id, _sessions.user_id AS user_id,
_sessions.updated_at FROM public._sessions _sessions
WHERE _sessions.updated_at > '" +(DT_STR, 20, 1252)#[User::MaxId] + "'"
This outputs:
SELECT _sessions.session_id AS session_id, _sessions.user_id AS user_id,
_sessions.updated_at FROM public._sessions _sessions
WHERE _sessions.updated_at > '13/10/2016 11:58:00'
However I need the date to output as so: > '13-10-2016 11:58:00'. it's the dashes and not the slashes.
How do I change my expression in order to meet this?
In SSIS, when you cast a DateTime type to string, it's calling the underlying .NET object's ToString() method. That will use your localization rules to produce the string.
You have dd/mm/yyyy hh:mm:ss due to a UK localization. I'd have mm/dd/yyyy etc for a US locale.
Therefore, if you want a special format, you have to build it yourself. I'm using StartTime as it made it easier for testing but, assuming MaxId is also a datetime type, it's a simple substitution
(DT_WSTR, 4) YEAR(#[System::StartTime])
+ "-"
+ RIGHT("0" + (DT_WSTR, 2) MONTH(#[System::StartTime]),2)
+ "-"
+ RIGHT("0" + (DT_WSTR, 2) DAY(#[System::StartTime]),2)
+ " "
+ LEFT(RIGHT((DT_WSTR, 24) (#[System::StartTime]), 11), 9)
The thing to note above is that to get the leading zero for day and month, we prepend a leading zero to the emitted string (for the month of March, we'll have 03, for October, it will be 010) and then only preserve the trailing 2 characters. This is much cleaner to troubleshoot than conditional logic based on the actual date.
I take a lazy route for time manipulation so I use the last 11 characters of the default time cast to get the time portion but then strip the AM/PM modifier off the end of that.
You have SSIS non-standard format for date DD-MM-YYYY. Try this expression
RIGHT("0" + (DT_STR, 2, 1252)DATEPART("dd", #[User::MaxId]), 2) + "-" +
RIGHT("0" + (DT_STR, 2, 1252)DATEPART("mm", #[User::MaxId]), 2) + "-" +
(DT_STR, 4, 1252)DATEPART("yyyy", #[User::MaxId]) +
+ " " + (DT_STR, 8, 1252)(DT_DBTIME)#[User::MaxId]
This builds your date. Second line adds time part.

Sum of dateTime values whose are in String format SSRS 2008

I have a Time variable returning from SQL query in dd:hh:mm format (day:hour:minute). Setting of this variable is:
...
Time = cast(SUM(b.Time) / 1440 as varchar)
+ ':' +
RIGHT('0' + cast(SUM(b.Time)/ 60 % 24 as varchar),2)
+ ':' +
RIGHT('0' + cast(SUM(b.Time) % 60 as varchar),2),
...
So Every Data has its own Time and I can show it in ssrs under Time column by just saying [Time]. My problem is that I would like to add the times of each data and show a TotalTime value. An example:
A ---- 00:02:20
B ---- 01:00:08
Total ---- 01:02:28
However, Sum[Time] does not work here because Time values are string. How can I make the sum progress in this case, I couldnt find a way to do it. I appreciate if someone helps!
THanks
You can write a formula to add the time. First convert your time (hh:mm:ss) to seconds then add the all the seconds and then again convert the total time in (hh:mm:ss) format.
Use this expression in SSRS to show formated dd:HH:mm minutes:
=FORMAT(FLOOR(Fields!Time.Value/1440),"00") & ":" &
FORMAT(FLOOR(Fields!Time.Value / 60 Mod 24),"00") & ":" &
FORMAT(FLOOR(Fields!Time.Value Mod 60),"00")
To get the total use this expression:
=FORMAT(FLOOR(Sum(Fields!Time.Value)/1440),"00") & ":" &
FORMAT(FLOOR(Sum(Fields!Time.Value) / 60 Mod 24),"00") & ":" &
FORMAT(FLOOR(Sum(Fields!Time.Value) Mod 60),"00")
Using this data arrangment in a tablix:
You will get this:
This works supposing you have a field of int data type that contains minutes.
Let me know if this helps.