Given a table with two dates and a date interval (i.e. 6 WEEKS), how can I count how many times this interval fits in this date interval?
In my PHP script I'm using a simple for loop, but I'd like to add this in a MySQL query.
A simple calculation would be to count the number of days between the dates and then assume a month has 30 days and a year has 365 days, but that's not always true.
How can I calculate with datediff and a date interval string?
When I understood you correctly, you need the timestampdiff() function.
The first parameter is the unit of the interval. The result is how many units are between the two dates.
Related
I need to count how many datetimes stamps occur within each hour.
Say I have this timestamp 2012-03-22 15:33:56.0 and 2012-03-30 08:22:45.0 in a column called date_time.
How do I put the hour (15 and 8) and count how many times those timestamps occurred (1 and 1) into separate columns!
Anything helps!
Please see the following DBFiddle, you can use the HOUR() function on the date time column and aggregate results : https://www.db-fiddle.com/f/2eKrampo67wNSnCajmwTok/0
i need a function to do something like this:
select datediff( '2016-01-01', '2015-01-01')
but ignoring some days, example :
calculate the interval between 2 dates only Mondays and Thursdays ignoring the other days
I do not want to make an select at the DB, only calculate the number of days between dates
sorry my English
Specs: I'm using MySQL 5.6 with SQLWorkbench, SequelPro on OSX Yosemite
Query background: I'm trying to correct a set of TIMESTAMPDIFF durations for weekends and bank holidays. I have 2 stored procedures which are giving me the number of Saturdays, Sundays and Bank Holidays between two dates - these are working fine. To get the corrected TIMESTAMPDIFF, I therefore multiply the number of Saturdays, Sundays and holidays by 24 to get the number of hours to be subtracted, then subtract that number from the TIMESTAMPDIFF.
Example: As an example, if timestamp A is 14:00 on Friday and timestamp B is 14:01 on Tuesday, the raw TIMESTAMPDIFF is 96:01:00. Assuming Monday is holiday and the weekend is 48:00:00, I want to subtract 72:00:00 from 96:01:00, to get the 'business day difference' of 24:01:00.
The problem: When I do something like "96:01:00" - "72:00:00" as date_sub_test, I get 24. I have lost all formatting, including the 01 minute. Duration are not DATETIME, as they don't correspond to calendar dates, so I can't use DATE_ADD / DATE_SUB.
The question: How should I subtract time from a duration, retaining formatting and relevant base 60 system eg 60 minutes in an hour, not 100?
Thanks in advance!
As Jaydee mentions in a comment:
Have you tried the TIMEDIFF function? https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
TIMEDIFF was what I was looking for. I also added in ABS() to make negative time differences positive, and MAKETIME() to create a time from an integer.
Use SEC_TO_TIME and TIME_TO_SEC like
SELECT SEC_TO_TIME(TIME_TO_SEC('96:01:00')-TIME_TO_SEC('72:00:00'))
you can use ABS like this
SELECT SEC_TO_TIME(ABS(TIME_TO_SEC('96:01:00')-TIME_TO_SEC('172:00:00')))
QUESTION
Is it possible the find the number of weeks in the current month using a MySQL query?
If you answer the above question, then you have answered my question. However, I have been downvoted before for not including context, so....
BACKGROUND (Warning: It Will Confuse You)
I advise not reading any further
Because of a MySQL parser we have written in an application, my hands are tied with the way in which I must approach writing queries.
Here's the scenario:
I need to average out the number of hours worked per week over the course of a month by an employee; however, I must approach this by generating a column returned by the query that can then be summed to give me the average (yes, that is a mouth full).
Here is what I mean (and one attempt):
SELECT ets.*, (ets.hours/4) AS AVG_HOURS
FROM employee_timesheet AS ets
WHERE ets.date_worked >= (NOW() - INTERVAL 1 MONTH)
Yes, the WHERE clause is not accurate, that will have to change, too (but that is the next step).
The application then SUMS(avg_hours). Consider that each ets record contains the hours worked per day, by dividing the (hours worked each day) / by the (floating representation of the number of weeks in the month), I should then get the average worked per week over the course of the month by summing the result of the division.
To represent the logic in pseudo-code:
SUM(days_hours/number_of_weeks)
You can use DATEDIFF function
SELECT DATEDIFF('2014-10-30','2014-12-29') AS DiffDate
it should return the days between two date
https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
To answer the question, yes you can find the number of weeks from a month by using SQL's
datepart(week, date).
If you need the current month's number of weeks you can do
Datepart(week, getdate())
Here's more information for DATEPART
To get the first day of the month you can use
DATEADD(month, DATEDIFF(month, 0, #mydate), 0)
Alright, I got it. If I want to get the current number of weeks elapsed in the current month, I can:
DAYOFMONTH(CURRENTDATE()) / 7
Is it posaible to write a query when you don't know any acutal dates? I need to compare transaction dates with funds available dates & update the funds available date by adding a day to the date.
I know MS SQL better than MySQL, but you still should be able to compare them using greater than and less than. If you want to modify the value, you'll want to use the DATE_ADD function (Date and Time functions reference). Comparing transaction date to the funds available date would involve something like WHERE TransTable.TransDate >= CustTable.FundsAvailDate, and then adding one day to the funds available date would be something like SET FundsAvailDate = DATE_ADD(FundsAvailDate, INTERVAL 1 DAY)