Getting the number of days excluding years between two dates - mysql

I want to get the number of days ignoring year between two dates in MySql.
I am using the following query, which works fine when the dates are more than a year apart.
select dayofyear(from_days((to_days('2015-11-20') - to_days('2014-11-15'))))
select dayofyear(from_days((to_days('2019-11-20') - to_days('2014-11-15'))))
correctly both return 5
However when the two dates are in the same year
select dayofyear(from_days((to_days('2014-11-20') - to_days('2014-11-15'))))
this returns 0. I think this is because MySql has a problem with dates in years less than 100 (or at least the dayofyear function has).
Is there any way around this?

Absent your willingness to specify what happens on leap years, one is forced to guess.
I think this works. http://sqlfiddle.com/#!2/043543/1/0
IF(timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st),MONTH(fi), DAY(fi)),'%Y-%m-%d')) >= 0,
timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st),MONTH(fi), DAY(fi)),'%Y-%m-%d')),
timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st)+1,MONTH(fi), DAY(fi)),'%Y-%m-%d'))) delta
It tacks the starting year onto the ending day and tries to compute the number of days. Then, if that comes up negative, it tacks the year after the starting year onto the starting day and computes the number of days.

You are complicating this issue for nothing;
There is a DATEDIFF function in MySQL that allow you to do that.
SELECT DATEDIFF('2006-04-03','2006-04-01');
That would return 2 (days).
To get the result regardless of the year, then apply % 365.
Edit : If you want to get the exact number of days considering leap years, you could substract to the number of days, the number of days for both year at 1st january (or any other date) :
SELECT DATEDIFF('2006-04-03','2006-04-01') - DATEDIFF(concat(year(yourDate), '01-01'), concat(year(yourDate2), '01-01'));
This will substract the total number of days between 1st january of each year to the total number of days.

Related

Displaying number of years worked since joined date

List all keepers that earn more than $37,000 a year at the zoo. Show the keepers name, the total salary for a year (assume 38hrs week) and the number of years they have worked at the zoo.
Select surname
, given
, RatePerHour*38*52
From Keeper
Where ‘ratePerHour’ *38*52 > 37000
This is what i've got so far, not sure how I'm supposed to display number of years worked?
Theres a "joined" table that shows date joined at the company.
I thought about trying '2019-03-08' - 'Joined' but that displayed the incorrect answer.
My best guess would be to utilize mySql's CURDATE() function to fetch the current date rather than hard coding today's date.
Then you can use DATEDIFF(%FutureDate%, %PastDate%) function to calculate the number of days worked and finally convert those number of days in a yearly representation.

sql counting per year and month, show months even when zero

So I'm counting articles per year/month between the start of the year and the current time:
SELECT Year(FROM_UNIXTIME(date)) as year
, Month(FROM_UNIXTIME(date)) as month
, Count(*) as `total`
FROM articles
WHERE date BETWEEN UNIX_TIMESTAMP(DATE('2017-01-01 00:00:00')) AND UNIX_TIMESTAMP(DATE('2017-05-17 12:00:05'))
GROUP
BY Year(FROM_UNIXTIME(date))
, Month(FROM_UNIXTIME(date))
The only issue, is that months that have zero, won't show up.
Is there an easy way around it?
The best solution I can think of is to do an inner join with a lookup table that has months 1-12 in them. Thus verifying there will always be a 12 month result set?
Possibly including a restriction for the current date month can not be surpassed, so you don't actually always get for the whole year.
look here:
Include missing months in Group By query

Getting info. about a date with no year in MySQL

I have a five character wide column of data that consist of a date with no year such as: 04-17 or 11-22. I am trying to create a MySQL query that will give me day of year for values in this column but am having problems because the values have no year. The closest I've come is
SELECT DAYOFYEAR(YEAR(CURDATE())-t.sdate) FROM tablex t;
but the concactenation does not seem to work. Any ideas how I can go about getting the Day of Year for date values such as these?
If you want concatenation, you have to ask for it explicitly with CONCAT().
SELECT DAYOFYEAR(DATE(CONCAT(YEAR(NOW()),'-',t.sdate)))
will do what you want, with reference to the present year. That makes a difference because some years are leap years.
You can't get an accurate DayOfYear from just a day / month pairing. You need the year to determine if it a leap year or not. You can fudge it, but it will be wrong every four years.

Total days from two dates in mysql

For the leave application, FROM date and TO date is given by selecting the dates from calender. I have these two dates in mysql.I want to calculate number of days for the leave. So Person applying leave from July 1 to July 5 th. So total 5 days.
But when i use DateDiff(to, date) it gives 4.
How can i get 5 days?
select datediff('2015-07-05','2015-07-01');
You may simply do +1, as you already notice, DATEDIFF excludes starting date, see example below:
select (datediff('2015-07-05','2015-07-01') + 1) as days
DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
example
SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
output : 1
or
SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
Output : 31
You can add manual plus 1
If you closely look at datediff function,
when you use two same dates, it will give you 0 days as a difference, not 1.
select datediff('2015-07-01','2015-07-01');
Will give you 0 days.
So its obvious that you will get one less day as it starts with 0 difference for same date.
datediff will return difference between two dates however you need to include both the dates while calculating the number of leaves. You should use value returned from datediff + 1.

Grouping months by quarter over multiple years depending on a dynamic start month

Using MySQL and PHP I am building a JSON array to populate a data table.
For the purposed of my question suppose the data table has the following columns:
Year: 2010,2011,2012,2013...<br/>
Month: 1,2,3,4,5...<br/>
Value: 100, 150, 200 etc...<br/>
The table structure cannot be altered and my solution needs come into the MySQL query
The data can be viewed either monthly, quarterly or yearly. Monthly and yearly is achieved easily through grouping by year and month.
Quarterly data can be grouped by calendar quarter (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec) by this group statement:
GROUP BY year, round((month/3)+0.3,0)
So where Jan, Feb and March might all have 100 for their value the summed result is 300, same for other months.
Now my problem comes when I want to group values by a financial quarter, for example a quarter that starts in Feb, or any other quarters.
I have a statement that works for the quarter grouping using two variables that can be accessed via the SQL query, start_year (i.e. 2014) and start_month (i.e. 2)
GROUP BY year, (((round(((((month-(start_month-1))+((year-start_year)*12))-((year-start_year)*12))/3)+0.33,0)/4)+4)-floor(((round(((((month-(start_month, '%m')-1))+((year-start_year)*12))-((year-start_year*12))/3)+0.33,0)/4)+4)))*12
which basically will assign a 0,3,6,9 value to each calendar month for the purposes of grouping.
In the financial year starting February this works fine for quarters 1-3, however breaks for the final quarter as it includes Nov and Dec 2014 data and Jan from 2015.
As a result I get 5 rows of data instead of 4.
This is because of the preceding GROUP by year clause, an important addition as we might want to generate a table that views sequential quarters for multiple years.
So what I am looking for is a way of grouping the years together by offsetting the start month.
So when the year starts in Jan it will group Jan-Dec but if we change that to starting Feb it will group Feb-Jan.
Any ideas, suggestions most welcome!
Regards,
Carl
I solved a similar problem just now (a Moodle report aggregating assignment scores by year and quarter) with something like this:
select year(from_unixtime(s.timemarked)) as year, quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter, count(distinct data1) as "tickets graded" from mdlassignment_submissions s where grade >= 0 group by year, quarter order by year, quarter;
The relevant part for what you're doing is quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter
As another commenter pointed out, MySQL has a quarter() function, but it doesn't do financial quarters. However, since (as I understand it, at least, based on consulting the relevant wikipedia page) financial quarters are just offset by 1, the % 4 + 1 at the end should convert it.