TIMESTAMPDIFF giving unexpected result - mysql

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

Related

Ms Access - Data type mismatch error when doing sum of few columns that will contain text symbols

I'm working on a simple RDBMS project. My database contains work hours per day of each employee. I have to calculate work hours of complete month. It is not a problem. But I have a problem, here it goes, each workday field contains either number of work hours or it may contain text such as if the employee was absent (symbol "A") for a particular day or it may contain symbol ("H") for holiday etc. Now my task is to calculate the 30 day work hours and exclude any symbol. But obviously the approach I am using will get me "Data Type Mismatch Error." So any body got any solution or any suggestion please. Thanks in advance!
I tried to use sum formula with NZ function, it doesn't work. Also tried Query with the same error.
Use Val:
TrueHours: Val(Nz([YourHourTextFieldName]))
That will return 0 (zero) for non-numeric entries, and that allows you to sum the values.

SUM expression with column and row scope

I work in lending and we are trying to get the percentages of purchases and refis to show for the months. The problem that I am having is that I cannot get the scope correct.
Here is the current list
Andhere are the row groups
I know this is definitely a scope issue - I just can't figure out a way to have it take into account the month and the year. I have had success with it doing the percent of the year value. I am trying to simply show the percent of purchases and Refis for a given month for a given year.
For example, If we have 60 purchases and 40 Refinances for January of 2019 - I want it to show 60% and 40%.
I have tried messing around with the row and columns groups every which way and I still cannot seem to figure it out. Any help would be largely appreciated.
= SUM(Fields!TOTALLOANAMT.Value, "Grouped_Loan_Purpose5") /
SUM(Fields!TOTALLOANAMT.Value, "App_Sent_Year1")
I have a fairly simple solution that you could use to avoid any headache involved with grouping. I would suggest simply using the values in the textboxes to calculate your percentages. You can just do something like the following.
=(ReportItems!PurchasesTextbox.Value / ReportItems!TotalTextbox.Value) * 100
=(ReportItems!RefinanceTextbox.Value / ReportItems!TotalTextbox.Value) * 100
Then you can just format as percentages and leave everything else alone.

MYSQL time diff

I'm trying to calculate time diff between two time fields. Because the fields are just time with no date, I can't use timestampdiff, so I'm using timediff() or subtime(). The only problem with those is when the second time is less than the first time, it returns a negative timediff for me. I understand it's taking the times as the same day times, but is there any way to get a behavior where it always calculates the time forward? For example, I have 23:00:00 and 07:00:00.
timediff('07:00:00','23:00:00') will return a negative value, because 23:00 is greater than 07:00, but I want to get 8 hours as the return. Is there any way I can do that?
You can achieve that with an if statement:
select if(dateA > dateB,
timediff(dateA, dateB),
addtime(timediff(dateA, dateB), '24:00:00.000000'))
This way, if the first date is smaller than the second, you add 24 hours to the difference.

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

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]

DATEDIFF function in SQL Server - executes differently in different cases

I am using SQL Server and just identify one problem in my case.
I have used the DATEDIFF function as:
select datediff(dd,'1935-12-07','2010-03-02')/365.00 ---> 74.28
select datediff(dd,'1935-12-07','2010-03-02')/365 ---> 74
select datediff(yy,'1935-12-07','2010-03-02') ---> 75
If you can observe that If I try DATEDIFF with 'dd' then I get the difference as 74/74.28.
But If I use it with 'yy' I get the diference as 75.
Why this is so? Means Why the difference came as 75 as it nearly approximate to 74.
I need this both function in different cases. But as it is behaving differently I am facing a lot of problems.
Suggest me some solution to this.
Thanks.
First case = You're implicitly casting to a float, shows the correct result
Second case = You're implictly casting to an int, which rounds down as part of the conversion (Floor value)
Third case = You're not casting anything at all, and DATEDIFF() returns a signed int, which rounds up (Ceiling value)
Solution: Do whatever you want, but keep it consistent throughout your code.
The datediff function checks for the difference of the date part you have specified, so it is correct in saying the difference in the year part of the two dates is 75.
Or to word it as the folks at microsoft do, it counts the number of boundaries crossed of that datepart. There are 75 year-boundaries crossed between the two dates in your example.
Have a look at this msdn page, it explains how it is supposed to work.