Sum of dateTime values whose are in String format SSRS 2008 - sql

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.

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]

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)

calculate time difference between two dates in ssis

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)

TIme - SQL (Minutes and Seconds)

I created a TIME table. This table has two columns: one for minutes and another one for seconds. I made their datatype as a Decimal.
Is there a way to create a derivative column where minutes and seconds are in this format mm:ss from my two columns?
IF NOT, How do I insert data into my minute column if its not a DECIMAL type? What type should it be?
Thank you!
Note I am using SQL server
Your comments make it sound like you're trying to do arithmetic on intervals (or durations).
SQL Server's time data type "Defines a time of a day. The time is without time zone awareness and is based on a 24-hour clock." You can't add two time values; 2 o'clock + 3 o'clock is literally nonsense. In SQL Server 2012 . . .
select cast('2:00' as time) + cast('3:00' as time)
Operand data type time is invalid for add operator.
Other dbms might return a nonsensical number.
Standard SQL includes a data type called interval, which supports the arithmetic and formatting you'd expect. So 2 o'clock + 3 hours would return 5:00:00 (5 o'clock). In the absence of support for the interval data type, store the most granular unit (seconds, for you) as an integer, and format it yourself for display. I might use a view, myself.
declare #val as integer;
-- 10:01:12, 10 hours, 1 minute, 12 seconds, in seconds.
set #val = (10 * 60 * 60) + (1 * 60) + 12;
-- Leading zeroes for minutes and seconds.
select #val as total_sec,
concat(#val / (60 * 60), ':', format((#val / 60) % 60, 'D2'), ':', format(#val % 60, 'D2')) as total_time
total_sec total_time
--
36072 10:01:12
As #mclaassen pointed out, I'd be curious why you aren't using the built in time data type.
That said, if you really want to build a time table by hand, then you can have a calculated column. Let's call it timeString.
alter table [time]
add timeString as (left('0' + cast([minutes] as varchar(10)), 2) + ':' + left('0' + cast([seconds] as varchar(10)), 2))
See https://msdn.microsoft.com/en-us/library/ms188300.aspx for documentation on calculated columns in SQL Server.

Convert seconds to '00:00:00' format - SQL Server

I have a report in which i have to calculate overall worked hours by any employee in a week. The report reflects hours more than 24 hours(or 86400 seconds). I want to convert all the seconds into "00:00:00" format keeping the format of the columns as datetime or time so that I can do further analysis using the time field.
Below is what I have tried so far:
Declare #WorkTimeInSeconds int
Set #WorkTimeInSeconds = 144000 **--This value is an example. Mostly values are above 86400 seconds**
Select ActualHoursWorked = convert(varchar,(#WorkTimeInSeconds)/3600)
+ ':' + right( '0' + convert(varchar,((#WorkTimeInSeconds) %3600)/60),2)
+ ':' + right( '0' + convert(varchar,(#WorkTimeInSeconds) %60),2)
Above code gives me what I need however the above converts the seconds in INT to varchar which is not what I need for the report. I need the output to be in datetime/time format which can give me results like '40:00:00' for the above example.
I hope I am able I have provided all the information. Please help as I am struggling with this from many days.
Expected Output: 40:00:00
Maybe try CONVERT style 108.
SELECT CONVERT(varchar(10),convert(varchar,(#WorkTimeInSeconds)/3600)
+ ':' + right( '0' + convert(varchar,((#WorkTimeInSeconds) %3600)/60),2)
+ ':' + right( '0' + convert(varchar,(#WorkTimeInSeconds) %60),2),108)