SSRS or VB Seconds into mm:ss or hh:mm:ss
I needed a way to display seconds in mm:ss. Many of the home grown functions I found online used strictly integers which rounded many of the times. I need something a little more accurate that eliminated rounding. From sql I used DateDiff() to get the number of seconds in my time span. Then in SSRS code behind I used the following sql to display it as mm:ss instead of a decimal.
Public Function ConvertSecondsToHourMinSec(ByVal intTotalSeconds) As String
Dim interval As TimeSpan = TimeSpan.FromSeconds(intTotalSeconds)
If intTotalSeconds = 3600 Then
ConvertSecondsToHourMinSec = TimeSpan.FromSeconds(intTotalSeconds).ToString
Else
If intTotalSeconds = 60 Then
'Remove preceding 00: for times less than an hour
ConvertSecondsToHourMinSec = Replace(TimeSpan.FromSeconds(intTotalSeconds).ToString, "00:", "")
Else
ConvertSecondsToHourMinSec = TimeSpan.FromSeconds(intTotalSeconds).ToString
ConvertSecondsToHourMinSec = Replace(TimeSpan.FromSeconds(intTotalSeconds).ToString, "00:00:", "00:")
End If
End If
End Function
SQL Code Behind Function
On the "Report Properties" page, select the "Code" tab.
SSRS Report - Use Function
To use the function in the SSRS report. Look for the "Value" property in the textbox property window.
=code.ConvertSecondsToHourMinSec(Fields!YOURSECONDS.value)
Microsoft Timespan()