SQL Server Reporting Services Date/Sum weirdness - reporting-services

I have 2 columns next to each other in a report I'm building. They contain the same structure, with one small difference. One column looks back in time a little further then the other. I have verified that the data is there to be found. A row group below returns the rows using the same structure. Column A returns the correct data. Column B does not.
Column A Expression
=IIF(Format(Fields!IncomingInvoiceDate.Value, "yyyy-MM-dd") > DateAdd("d", -31, Now()), Sum(Fields!Value.Value), 0)
Column B Expression
=IIF(Format(Fields!IncomingInvoiceDate.Value, "yyyy-MM-dd") >= DateAdd("d", -61, Now()), Sum(Fields!Value.Value), 0)
I've tried every single variation I can find, and nothing returns the correct data. I'm on SQL Server 2012. Thanks for any help.

Put your IF inside the SUM instead of the other way around.
Something like this should work. You'll want to change both the 30 day and 60 day expressions to fit this pattern.
Sum(
IIF(Format(Fields!IncomingInvoiceDate.Value, "yyyy-MM-dd") >= DateAdd("d", -61,Now()), Fields!Value.Value, 0)

It turns out nothing has worked, and I redesigned the query completely. Thanks all.

Related

Adding two date stamps into a single SSRS expression/cell

This is all done in Microsoft SQL Server Report Builder
So I have a large data set that contains Work orders and then their 'type/craft'. Each craft is then broken down into each row so that you can see how many work orders are still open in the certain craft. At the top of the page, It list, WO's 1-2 Past Due, WO's 3-5 Past Due, WO's 6-10 Past Due... etc, till you reach 30 days+
I then have an expression inserted that will tell you what date the 1-2 days is... However, I am having trouble making the expression value be 2 dates or an in between date.... For example, I have =DateAdd("d", -1, Now) inserted which will give me the date from 1 day back, however, I would ALSO like it to show 2 days back.... So instead of ONLY saying 6/13/2018... it would say 6/12/2018 to 6/13/2018
I guess I could go back and edit my SQL code to automatically do the dates, however I thought it would be easier to use the report system.
You would want your expression to be something like this:
=DateAdd("d", -2, Today()) & " to " & DateAdd("d", -1, Today())
If you need the time as well you would want to use Now instead of Today, but based on your question it seems you are only interested in the dates, so this should return exactly what you are looking for.

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"))

SSRS remove #ERROR from working days expression results

Im a bit stumped at the moment. I've created an expression that calculates the working days between 2 dates which works fine.
=(DateDiff(DateInterval.day,Fields!TCY_START_DATE.Value,
Fields!TCY_TENANCY_VISIT_DATE.Value))
- (DateDiff(DateInterval.WeekOfYear,Fields!TCY_START_DATE.Value,
Fields!TCY_TENANCY_VISIT_DATE.Value)*2)
- IIF(Weekday(Fields!TCY_START_DATE.Value,1) = 1,1,0)
- IIF(Weekday(Fields!TCY_START_DATE.Value,1) = 7,1,0)
- IIF(Weekday(Fields!TCY_TENANCY_VISIT_DATE.Value,1) = 1,1,0)
- IIF(Weekday(Fields!TCY_TENANCY_VISIT_DATE.Value,1) = 7,1,0)
The issue I have is that if the TCY_TENANCY_VISIT_DATE is blank then the results of this expression is #ERROR.
I need to tweak this expression so it will basically say if the visit date is blank then display nothing but still incorporate it into my working days formula
This is because SSRS evaluates the entire expression before it tries to find the actual path for the values it has. Therefore it will try and find the Weekday of Fields!TCY_TENANCY_VISIT_DATE.Value, even if it is blank, and will therefore throw an error.
The solution to this is to handle all your potential blank or empty values from the beginning. In this instance changing the line
IIF(Weekday(Fields!TCY_TENANCY_VISIT_DATE.Value,1) = 7,1,0)
to
iif(iif(isnothing(Fields!TCY_TENANCY_VISIT_DATE.Value),
"",
Fields!TCY_TENANCY_VISIT_DATE.Value), 1) = 7,
1,
0)
should do the trick. You may also need to repeat this process for your other values if there is a chance they could be null.

SSRS- Issue with iif containing monthname function

I have a data set that I am trying to force everything >12 months ago into a single group. I am trying to do this with a SQL case when that assigns all months and year >12 months ago a int value of 0 but when I try to put this into SSRS it doesnt work.
I have been trying to use this formula but it does not work unless I remove the 'MonthName' function.
=iif(Fields!ASGN_SUB_DUE_MONTH.Value > 0,monthname(Fields!ASGN_SUB_DUE_MONTH.Value) & " " & Fields!ASGN_SUB_DUE_YEAR.Value
,"More than 12 months ago")
But this formula works
=iif(Fields!ASGN_SUB_DUE_MONTH.Value > 0,
monthname(iif(Fields!ASGN_SUB_DUE_MONTH.Value > 0 and Fields!ASGN_SUB_DUE_MONTH.Value < 13, Fields!ASGN_SUB_DUE_MONTH.Value,1))
& " " & Fields!ASGN_SUB_DUE_YEAR.Value
,"More than 12 months ago")
Does anyone know why and how I can resolve. The second function works but I'm not comfortable leaving it in the reporting incase the second part of the imbedded iif ever functions.
Thanks in advance
The issue is related to my case statement and how the iif processes in SSRS, from my subsequent research.
The true statement needs the imbedded iif because its evaluating all entries vs all parts of the expression, so even when i have "Month" values of -1 or 0 that wouldn't hit the iif portion with MONTHNAME() its still evaluating it and causing an error.

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