Datediff fuction in Access produce answer of 1 instead of 0 for same day - ms-access

I am trying to get a total number of days hired using the DateDiff function is access.
I currently use:
=DateDiff("d",[hired_from],[hired_to])
To get the difference between two dates, however if the two dates selected are the same it will produce an output of 0, I would like it to produce an output of 1 when the two dates selected are the same, thanks.

It doesn't make much sense as the difference between two identical values always will be zero.
So you probably mean:
=DateDiff("d",DateAdd("d",-1,[hired_from]),[hired_to])
or just add one to the count:
=DateDiff("d",[hired_from],[hired_to])+1

I ended up making this work by using an if statement as shown:
==IIf(DateDiff("d",[hired_from],[hired_to])=0,1,DateDiff("d",[hired_from],[hired_to]))

to get the difference of two dates (which is only a format for an integer value), you have only to subtract the two dates and make sure that the result format is an integer.
Variable = [hired_to]-[hired_from]

Related

Access query using calculation

I'm trying to make a query using a calculation with Date().
I have a field named [Currentordue] and a field named [duedate]. What I'm trying to accomplish is making a query to limit the results by "if [currentordue] equals "due" or if [duedate] minus today's date is less than 30 days."
I've tried a few different ways but always seem to end with either an error or with no results showing (which would be an error as well since I know there are fields that are due).
Any and all help would be appreciated.
Here is a way to use two different date conditions:
SELECT Table1.Currentordue, Table1.duedate, DateDiff("d",[duedate],Date()) AS Expr1
FROM Table1
WHERE (((DateDiff("d",[duedate],Date()))<30)) OR (((Table1.Currentordue)=[duedate]));

TIMESTAMPDIFF giving unexpected result

Why is
TIMESTAMPDIFF(MONTH, '2015-12-25', '2016-02-24')
giving me 1? I would expect 2016-01-25 to be 1.
My guess is that it is actually returning something like 1.999 months and it is being simply rounded down.
How can I work around this? Or is there another function to use.
I tried PERIOD_DIFF but it does not take into account days
PERIOD_DIFF(DATE_FORMAT('2016-02-24','%Y%m'),DATE_FORMAT('2015-12-25','%Y%m'))
According to the documentation, the unit for the result is an integer, in your case it will return the whole number of months between the two dates, which is one (as the second month is not yet completed).

SSRS Expression with parameter in WHERE clause

In SSRS, I'm trying to calculate the average number of months a client used a program. The programID is the parameter for the whole report. I'm trying to achieve this (not written with real syntax):
=Avg(Fields!length_of_stay.Value, 0))/30.0 WHERE programid = #ProgramID
Using this question, I came up the the following code which is producing an incorrect answer. I tested in SSMS to get the actual values to compare to SSRS results.
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, 0))/30.0
The "/30" is used since the value is in days and I need months. I think the issue is using the parameter value chosen; this is my first report trying to calculate expressions with parameters.
Avg returns the average of all non-null numeric values. 0 is not null so it gets included in the average, distorting your result for every row with a different PragramId. Try using Nothing instead:
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, Nothing))/30.0

SSRS Avg function returns result that is different than expected

Currently I am trying to average a set of numbers, but with certain conditions.
Is it possible to use an iif() within an avg() and return the correct result?
Furthermore, as of now my computations return a decimal returned to a power (8.9267....E -05).
I tried circumventing the AVG function by conditionally summing and then dividing by a conditional count but it gives me the same results.
Can someone explain why this is returned and offer help?
Currently I have:
=avg(iif((This_case) AND (That_case) AND (This_conditional)
, Fields!ResponseRate.Value
, 0))
Essentially I want the average ResponseRate if certain conditions are met.
The sum function works fine for the conditions but the average doesn't.
You can definitely use IIf within Avg and get the correct results.
Do you want to exclude the False values from the calculation entirely?
In your example you're still including them, just setting them to 0 and hence still including them in the calculation. This might explain your unexpected results.
If you want to exclude them entirely use Nothing instead of 0.
Edit after comment
You can nest an expression in another IIf statement and check for NULL values using IsNothing.
Say your condition average expression is:
=Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing))
You can return 0 for NULL values with something like:
=IIf(IsNothing(Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
, 0.0
, Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
I'd like to add my two cents to this one, a little late, but also valuable. I was able to use the above code to Average out a data set if another record appears X number of times.
=AVG(IIF(Count(Fields!AcctNo.Value, "AcctNo1") = 2, Fields!Limit.Value, Nothing))
So, if the acctno field appears 1 time, avg the limit field for that row group. Ian's example was very helpful

Use a summary field in an expression in SSRS reports

I have the details of my report being summed up in a summary expression, all works fine. The fields are decimal values of hours worked. Thus, the summary value is also a decimal value. I'd like to access the summary value and convert it to hours / minutes. I've labeled the express as "WorkTimeSum", but can't seem to get a handle to it, however. Fields! obviously won't work since it is a summary expression. I was thinking ReportItems! should work, but to no avail. How can I use this expression field (in a summary row) in an expression in the same summary row?
If I've understood correctly, you're asking how to reference the textbox containing the total work hours value so that you can convert it to hours and minutes using an expression in a different textbox?
You can use either ReportItems! e.g.
=ReportItems!Textbox20.Value)
or ReportItems("") e.g.
=ReportItems("Textbox20").Value
to reference the value of another textbox. Be careful with the names as they are case sensitive.
You can use aggregate functions in any expression. For example, in any header rows you can use the following expression to determine the total hours value:
=Floor(Sum(Fields!hours.Value))
Sum(Fields!hours.Value) is just the total hours in whatever context, e.g. the group total if it's a group header row; you can use this expression as an input in any other expression you require.
It sounds like your issue wasn't the conversion itself, so hopefully this points you in the right direction. If you need further information please specify.