SSRS default param expression today() vs now() - reporting-services

I have a parameter of type date/time. I am creating a default value as expression. When I put
=IIF(Hour(TODAY()) < 1,
DateAdd("d", -1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
it works well and return a value (although always the same, since the hour is always 0 (12AM))
But I want to base this default value on the CURRENT date time attime of report. So when I replace today() with NOW(), it errors out and says that it "doesn't have expected type".
=IIf(Hour(now())<1,
DateAdd("d",-1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
Thanks
I tried multiple formulas to convert now() into something, but nothing worked.

It seems as though the expression didn;t evaluate the second part of the IIF because the HOUR(Today) was always 0. The Format would convert the date into a string which would cause the error. Evidently, the DateAdd was converting the string back into a date in order to subtract the day.
I think you can do without all the converting.
=IIF(Hour(NOW()) < 1,
DateAdd("d", -1, Today()),
Today())

Related

Greater than / Less than date/time table expression

I have the following expression in a MS-access table:
IIf([End Date/Time]>="12/8/2016 6:00:00",1,0)
12/08/2016 18:15:00 will return a '1', however
12/08/2016 14:23:29 returns a '0'
I'm assuming this is an issue with AM/PM. I tried putting '6:00:00 AM' in my expression but no change.
Also I would like to replace '12/8/2016' with 'yesterday' but date()-1 doesn't seem to be working.
EDIT: I figured out that the time needs to be '06:00:00'. That yield the correct dates. Still don't know how to get this automatically (ie yesterday at 06:00)
Thanks
Your issue is that you threat dates as strings. Use date always, no exceptions.
Further, if your field is not a date value, you must convert it.
Thus, this will work:
IIf(DateValue([End Date/Time]) >= #2016/12/8 6:00:00#, 1, 0)
and this:
IIf(DateValue([End Date/Time]) >= Date() - 1, 1, 0)
and this:
IIf(DateValue([End Date/Time]) >= DateAdd("d", -1, #2016/12/8 6:00:00#), 1, 0)
2 things. First, I believe you need to convert your string to a datetime. I think think you're getting wonky results because it's trying to compare them as a different format. Like a strings or numbers. To format as a date time you use a format fucntion:
Format("12/8/2016 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm")
Second, to add a date you need the DateAdd function.
DATEADD('d',-1,"12/8/2016 6:00:00AM")
'd' defines the -1 as a 'day' being added.
So, putting it all together:
DATEADD("d",-1,Format("12/8/2016 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm"))
And finally, if you want the want the rolling yesterday, and not perpetually 12/7/2016 (since you would just use that date if it was the case), you need to get today's date with this function:
Date()
So, throwing this into our mix we get:
DATEADD("d",-1,Format(DATE() & " 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm"))

Error when trying to DATEADD milliseconds in SSRS

So I have an enddate parameter in my report and I have the following code under Default Value to get me the last day of the previous month.
=DateAdd("d", -1, DateSerial(Year(Now()), Month(Now()), 1))
This works fine, returning 04-30-2016. That's fine and dandy but when I go to use the Subscription piece in SSRS it has the datetime field as: 04-30-2016 12:00:00 AM. So I tried doing the following to my code to have it shave off three milliseconds:
=DateAdd("ms", -3,DateAdd("d", 0, DateSerial(Year(Now()), Month(Now()), 1)))
When I try to run my report I get the following error:
The DefaultValue expression for the report parameter "enddate" contains an error: Argument 'Interval' is not a valid value. (rsRuntimeErrorInExpression)
How do I fix this error and get my report running as it should?
The AddMilliseconds function will work for you in this case.
=DateAdd("d",-1,DateSerial(Now.Year,Now.Month, 1)).AddMilliseconds(-3)
This gives you the last day of the previous month substracting three milliseconds.
Let me know if this helps.

Comparing todays date against first day of the week

I am trying to set a default parameter value in SSRS report. I want to test the current date to see if it equals the first day of the week (in my case Monday). If it is the first day of week, then I want the default value to be current date minus 2 days, if it is not the first day of the week then I want the default value to be current date minus 1 day.
I seem to have a syntax problem but it doesn't tell me where. My parameters are StartDate and EndDate.
this is what I've tried:
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today(),DateAdd("d",-1,today())
this is the generic error I get:
The value expression for the report parameter 'StartDate' contains eror:[BC30201] Expression expected.
Where am I going wrong?
You are trying to use Sql syntax in a SSRS VBA expression. SSRS VBA allows very similar expressions for date manipulation to Sql, the main difference being the use of the DateInterval enum.
So your expression needs to use VBA syntax:
=IIF(Weekday(Today, FirstDayOfWeek.Monday) = 1, DateAdd(DateInterval.Day, -2, Today), DateAdd(DateInterval.Day, , -1, Today))
It appears that you are missing a closing parentheses after the first logical part of the if statement and another to close the statement.
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today()),DateAdd("d",-1,today()))

The added or subtracted value results in an un-representable datetime

I have the following expression to check parameter date values and replace from date if necessary:
=IIF(Parameters!FromDate.Value is nothing,
IIF(Parameters!ToDate.Value is nothing,
DateAdd("d",-30,Now()),
DateAdd("d",-30,Parameters!ToDate.Value)),
Parameters!FromDate.Value)
Only when the todate is null, an error will get appear:
The added or subtracted value results in an un-representable datetime.
Did anybody face such a problem?
The problem is that IIF is a function, not a language construct so it evaluates both parameters before passing them to the function. That means DateAdd("d", -30, Parameters!ToDate.Value) gets evaluated even when Parameters!ToDate.Value is Null, thus giving you this error.
Try this instead:
=IIF(Parameters!FromDate.Value is Nothing,
DateAdd(DateInterval.Day, -30, IIF(Parameters!ToDate.Value is nothing, Today, Parameters!ToDate.Value)),
Parameters!FromDate.Value)

Report Builder - Set datetime parameter

I have a report that has parameters StartDate and EndDate. I would like the EndDate parameter's time part to default to the end of the day when selected from the drop down.
For instance, if a user selects 5/15/2008 from the dropdown, the value shown in the box should be '5/15/2008 23:59:59' not '5/15/2008 12:00:00'
Its pretty easy to do this in .Net using the event model and a line of code but in Report Builder 2.0, what would I need to do?
Is there code that I need to write for this or did I miss some funky expression that could handle this?
Thanks.
AboutDev
I would suggest setting the default parameter in the Report Parameters section. You can get to this from Report > Report Parameters.
This allows you to set a non-queried default. There you can enter an expression like
=DateAdd(Microsoft.VisualBasic.DateInterval.Second ,-1,dateadd("d",1,Today))
That should give you a default for the end of today.
Edit: Really only useful for a single default value.
It's been awhile since I've used SSRS, so bear with me. You'll have to do a little translation, but here's what I've done in the past.
When you define your EndDate parameter, create an additional parameter named EndDateEOD, after EndDate in your list of parameters. Make this parameter a hidden value, and set it to the last moment of the day, similar to the way that Jeremy calculates it.
Then you can use #EndDateEOD in your report query where you have #EndDate now.
When StartDate is selected, you could have EndDate default to its value, so that EndDateEOD will automatically be set to the end of the start date.
Use the parameter in a DATEADD() expression in your dataset.
Rather than
...WHERE end_date = #end_date
do something like this:
...WHERE end_date = DATEADD(ms, -3, #end_date + 1)
That will go forward a day (the +1), then go back 3 milliseconds, to the last instant of the day recordable by a datetime.
You can do something like this:
=CDate(Parameters!StartDate.Value + " 23:59:59")
The part of Parameters!StartDate.Value can be any date but not the EndDate.Value itself. Eg:
- Today()
- Last day of the month from start date:
=CDate(DateSerial(Year(Parameters!StartDate.Value), Month(Parameters!StartDate.Value) + 1, 0)+" 23:59:59")
Hope this help.