SSRS expression - convert Decimal to HH:mm - reporting-services

In SSRS dataset I have "Hours" column with values e.g. "8", "8,5" etc.
And I need convert it to HH:mm format.
So ideal example:
8 -> 08:00
8,5 -> 08:30
And special question - is it possible if I have "XYZ" column contains "168" (hours) get 168:00 using SSRS expression?
Thank you!

usually the best way is to add your time as seconds to a date and then get the time.
=FORMAT(DATEADD("s", Fields!HOURS.Value * 3600, CDATE("1900-01-01")), "hh:mm")
Unfortunately, this only works with less than 24 hours. For your 168 hour possibility, you'd need to calculate each time field. Luckily you just need to calculate the minutes.
=INT(Fields!HOURS.Value) & ":" & RIGHT("00" & INT(Fields!HOURS.Value * 60 MOD 60), 2)

You can use the following expression
= Cstr(Cint(Fields!hours.Value)) & ":" & Format(Cint((Fields!hours.Value Mod 1)*60),"00")
The first part of the expression will take the integer part and convert it to string.
The second part of the expression will take the decimal part, multiply it by 60 (minutes) and format it as two digits.

Related

Ms Access how to convert a string HH:MM to numbers or time for calculation

in my ms access query i have a column named [Hours & Minutes]. the column values are taken as "(((tblA.TotalMins) &':'&(mid (tblA.TotalHours,4,2))) as [Hours & Minutes]". it is a string type i want to sum it so i want to convert it into a HH:MM format for calculation or any format for calculation
Your expression doesn't make sense, as it seems to interchange hours:minutes to minuttes:hours.
If your field holds text like "22:45", this can easily be converted:
DateValue([TextTime]) As TrueTime
If you have a field for hours and another for minutes, use TimeSerial:
TimeSerial([TotalHours],[TotalMinutes],0) As TrueTime

How do I Sum Hour and Minutes SSRS 2012?

I have a column in my report that contains text values like:
01:30
00:45
02:15
And so on. How do I get a total like this?
04:30
As a total time spent. I am trying this Expression -
= FLOOR(Sum(Cint(Left(Fields!est_pack_time.Value,2)), "DataSet1") ) & ":" & RIGHT("0" & (Sum(Cint(Right(Fields!est_pack_time.Value,2)), "DataSet1") MOD 60), 2)
This gives me something close but it is still not right. I am summing the total time across the entire dataset.
You were on the right track - your expression is just not carrying the extra hours from summing up the minutes into the total hour time.
The updated expression will be something like this:
=Right("0" & Sum(CInt(Left(Fields!est_pack_time.Value,2)), "DataSet1")
+ Floor(Sum(CInt(Right(Fields!est_pack_time.Value,2)), "DataSet1") / 60),2)
& ":" & Sum(CInt(Right(Fields!est_pack_time.Value,2)), "DataSet1") Mod 60
This is taking the total from the hours, adding the overflow from the total minutes, then concatenating the minutes to this string. It's also padding the hours with a leading zero to make sure the string is always 5 characters.
Works for me in a quick test:
Finally, it probably goes without saying that if at all possible it's best to move away from storing time durations in a text format, but of course that might not be possible in your case.
=split(TimeSpan.FromTicks(Sum(Fields!TotalHours.Value)).TotalHours,".").GetValue(0) &":"
& Format(TimeSpan.FromTicks(Sum(Fields!TotalHours.Value)).TotalMinutes mod 60,"00")
=replace( left(TimeSpan.FromTicks(Sum(Fields!TotalHours.Value)).TotalHours,3)&":"
& right(TimeSpan.FromTicks(Sum(Fields!TotalHours.Value)).TotalHours,3)*60,".","")

SSRS: Convert decimal to hours and minutes

I have a report where I want to display time in hours and minutes (17hrs 30 mins)
The dataset returns this value in decimals (Eg 17.5)
How would I convert this decimal to the format specified above (17hrs 30 mins). Is there some kind of built in function that can do this easily?
This works for your particular example:
=Floor(Fields!MyValue.Value)
& " hrs "
& CInt((Fields!MyValue.Value - Floor(Fields!MyValue.Value)) * 60) & " mins"
You may need to tweak for all possible scenarios but this should be a good starting point.

How to set the year part of a field using a query

Evening,
I have created a query that is suppose to change ONLY the year part of a Date/Time field to 1900 when a person is 89 years or older. The query that follows compiles fine but when run it complains about a Type Conversion failure and removes the entire value from the records affected.
The query:
UPDATE tblTestForDOB
SET tblTestForDOB.[PT_BirthDate] = DateValue( (day([PT_BirthDate])/month([PT_BirthDate])/1900) )
WHERE Year(tblTestForDOB.[PT_BirthDate]) <= Year(Date())-"89";
According to the MS Help (F1 over the function):
The required date argument is normally a string expression representing a date from January 1, 100 through December 31, 9999. However, date can also be any expression that can represent a date, a time, or both a date and time, in that range.
Is that not what I'm doing? I also tried placing the " " & before the values inside the DateValue function and that did the same thing
(to ensure that it was a string that was passed)
So how do I go about it? Should I use CDate to convert the value to a Date and then proceed that way? If so what is the correct syntax for this?
Thanks
P.S The field is of Short Date format. Also note that I don't want to take the long way around and use VBA for the whole thing as that would involve opening record sets and so on...
It appears you're trying to give DateValue a string, but that's not what's happening. There may be more going on that I don't understand, so I'll just show you an Immediate window session which may contain something you can build on.
PT_BirthDate = #1923-6-1#
? PT_BirthDate
6/1/1923
? DateDiff("yyyy", PT_BirthDate, Date())
90
' this throws error #13: Type mismatch ...
? DateValue( (day([PT_BirthDate])/month([PT_BirthDate])/1900) )
' it will work when you give DateValue a string ...
? DateValue("1900-" & Month(PT_BirthDate) & "-" & Day(PT_BirthDate))
6/1/1900
' or consider DateSerial instead ...
? DateSerial(1900, Month(PT_BirthDate), Day(PT_BirthDate))
6/1/1900

How to display a time span of seconds in hh:mm:ss format in Reporting Services

In MS Reporting Services 2008, I have a field that is a duration stored as seconds. Is there a slick way to get it into hh:mm:ss format in a group section of the report?
If you just want to display it, convert in an expression for the Value of the textbox:
=Format(DateAdd("s", Fields!MySecondsField.Value, "00:00:00"), "HH:mm:ss")
If you want to do calculations on it, convert the seconds to a DateTime in your dataset. Using SQL:
SELECT DATEADD(ss, MySecondsField, '1900-01-01') AS SecondsAsDateTime
FROM TimeTable
In Linq this would be something like:
var qry = from Q in t.TimeList
select new
{
SecondsAsDateTime = DateTime.Today.AddSeconds(Q.MySecondsField)
};
Then you can just format it as a normal DateTime.
If you need to work with times longer than 24 hours (Chris Latta's solution will wraparound in these cases), then there are a couple of solutions.
Simple Formula
If you want to just use a formula on the field such the following from this thread, (which also links back to this question)!
=int(sum(Fields!Sec_Online.Value)/3600) & ":" & int((sum(Fields!Sec_Online.Value) Mod 3600)/60) & ":" & (sum(Fields!Sec_Online.Value) Mod 3600) Mod 60
If you need to pad your value to 2 characters you can wrap a RIGHT("0" & {X}, 2) around each sub-section, where {x} indicates one of the individual calculations in the above formula.
Code Behind
Another approach, also suggested in this thread, is to use TimeSpan.FromSeconds(doc), and there is an implementation of that on this blog, using custom code behind in the report.
I ended up using the custom code approach (as I had lots of fields sharing this), and combining it with something more like the first method as I didn't want days to start appearing I just wanted hours to count up bigger than 23.
I added some custom code to the report as follows which pads all values to at least 2 characters, and allows hours to hours count up > 23.
Public Function ConvertSecondsToHourMinSec(ByVal intTotalSeconds) As String
Dim hours As String =INT(intTotalSeconds/3600)
If Len(hours) < 2 Then
hours = RIGHT(("0" & hours), 2)
End If
Dim mins As String = RIGHT("0" & INT((intTotalSeconds MOD 3600)/60), 2)
Dim secs AS String = RIGHT("0" & ((intTotalSeconds MOD 3600) MOD 60), 2)
ConvertSecondsToHourMinSec = hours & ":" & mins & ":" & secs
End Function
and then called this from each cell in questions as follows:
=code.ConvertSecondsToHourMinSec(Fields!MyField.Value)
I hope this helps someone else!
Use expression below, replace bold with your field containing the seconds variable.
=DateAdd(DateInterval.Second, Sum(Fields!totalDuration.Value), CDate("1900-01-01 00:00:00"))
I always apply formating in the textbox properties-
H"h "m"m "s"s"
will show as "2h 16m 5s"
use a function like this:
Public Function ConvertTsToHMS(myValue As long) As String
Dim ts as TimeSpan = TimeSpan.FromSeconds(myValue)
return Int(ts.TotalHours).ToString("00") + ":" + Int(ts.Minutes).ToString("00") + ":" + ts.Seconds.ToString("00")
End Function
I've used the idea of Xan recently and due to the fact the numbers of seconds I have are quite large - I ran out of integer type limit. Therefore I designed a different approach - maybe someobody will find it useful :)
I needed to create calculated fields for Hours, Minutes and Seconds - used following formulas:
XXXNumberOfHours = int(Fields!TimeInSec.Value/3600)
XXXNumberOfMinutes = int((Fields!TimeInSec.Value Mod 3600)/60)
XXXNumberOfSeconds = =((Fields!TimeInSec.Value Mod 3600) Mod 60)
Then I created an expression to display the seconds in HH:MM:SS (without days - did not needed that) format:
(Sum(Fields!LWTNumberOfHours.Value)+int((Sum(Fields!LWTNumberOfMinutes.Value) + int(Sum(Fields!LWTNumberOfSeconds.Value)/60))/60))
& ":" &
right("0" & (Sum(Fields!LWTNumberOfMinutes.Value) + int(Sum(Fields!LWTNumberOfSeconds.Value)/60)) Mod 60, 2)
& ":" &
right("0" & Sum(Fields!LWTNumberOfSeconds.Value) Mod 60, 2)
In the above lines you can see that line (1) calculates number of hours, line (3) calculates number of minutes and line (5) calculates number of seconds.
Of course you can notice that additional calculations are made to get the number of full minutes out of XXNumberOfSeconds and same applies to Minutes/Hours. This could be also done in the calculated fields already (and maybe it would even be more right to do so :) ) - however I preferred to use the above approach.
This way I was able to still format very large numbers of seconds that exceeds integer typesize.