SSRS: Hour() for 24:00

时间:2016-10-20 12:36:01

标签: sql-server function reporting-services ssrs-2008-r2

I was using Hour(Fields!tempo.Value) for get the hour from a field (tempo). The problem came when I got for first time "24:00" and the report shows the '#error'.

The description in 'Hour' function said:

Returns an Integer value from 0 through 23 representing the hour of the day

So, I changed the expression to

=IIf(Fields!tempo.Value = "24:00", 0, Hour(Fields!tempo.Value))

but yet I get the #error. Then I tried:

=IIf(Fields!tempo.Value = "24:00", 0, 1)

and correctly I get "0" for 24:00 values and "1" for the rest.

  1. I don't understand why Hour() won't work under IIf().
  2. Maybe exist some better workaround for this case and I don't know it.

Thanks in advances

EDIT:

The idea is get all events which started between 2 hours every day (for filter events from the morning, afternoon and night):

=IIf(Hour(Fields!tempo.Value) >= 22 Or Hour(Fields!tempo.Value) <= 6, SHOWTHIS, Nothing)

1 个答案:

答案 0 :(得分:1)

if you don't need to perform operations with the hour you can work with strings to get the hour, also if your dataset returns a string this is the simplest solution (IMO).

Try using this expression:

=IIF(LEFT(Fields!tempo.Value,2)="24","0",REPLACE(LEFT(Fields!tempo.Value,2),":",""))

If you still want to get the hour using HOUR function you have to validate twice your field, in the IIF and inside the HOUR function:

=IIF(Fields!tempo.Value="24:00",
0,
HOUR(IIF(Fields!tempo.Value="24:00","0:00",Fields!tempo.Value))
)

Let me know if this helps.