I am relatively new to Access - in a Query how do I calculate the number of years between a historic date and today- date is in the format 00/00/0000
You are looking for DateDiff("yyyy", Now, TheDate) this will return the year number between TheDate and the current date.
You can use d or ww (instead of 'yyyy') if you want to learn the amount of day or week between the dates.
For detailed information you can look into this link:
https://support.office.com/en-us/article/DateDiff-Function-e6dd7ee6-3d01-4531-905c-e24fc238f85f
Related
How to find how many week in particular month and year using mysql, I have only year and month no date value.
Example find out number of week year = 2017 and month = August
I search many times but all examples are given with date.
I want to calculate start date and end date for the particular given week in the month , (assuming my week starts on monday and ends on sunday).
So, What I want is if I select any week number lets say 1,2 ,3 or 4 I should be able to calculate the start date for that particular week and end date .
In addition to the week number for that particular month , I will also give an input of year and month.
selection of year month and week number will be from HTML page through a drop down select option.
And I am looking for some efficient way of doing this .
Use Moment.js
Get the value of week number
var weeknumber = moment("07-27-2017", "MM-DD-YYYY").week(); console.log(weeknumber);
I currently have a query that is getting the records where their deadline is less than 3 months away. The deadline is stored as a Date, but the year is not important as this record should flag up every year.
My query:
SELECT client_name
FROM client
WHERE MOD(DAYOFYEAR(deadline) - DAYOFYEAR(CURDATE()), +365) <= 90
AND DAYOFYEAR(deadline) > DAYOFYEAR(CURDATE())
Apologies if there’s errors in the syntax as I’m writing this from memory – but it does work.
It works up until a deadline is in the first quarter and the current date is in the final quarter then it no longer returns the record. How do I get around this?
So the query needs to return the records that have a deadline within 3 months of the current date. The Year in the deadline date should be ignored as this could be years ago, but it is the day and month of the deadline that is important.
Or is the problem the date I am storing? Should I update this each year?
Thanks
One approach is to use a conditional test like this:
WHERE CONCAT(YEAR(NOW()),DATE_FORMAT(d.deadline,'-%m-%d'))
+ INTERVAL CONCAT(YEAR(NOW()),DATE_FORMAT(d.deadline,'-%m-%d'))<NOW() YEAR
< NOW() + INTERVAL 3 MONTH
We can unpack that a little bit. On that first line, we're creating a "next due" deadline date, by taking the current year, and appending the month and day value from the deadline.
But there's a problem. Some of those "next due" deadline dates are in the past. So, to handle that problem (when the 3 month period "wraps" into the next year), we need to add a year to any "next due" deadline date that's before the current date.
Now, we can compare that to a date 3 months from now, to determine if the "next due" deadline date is in the next 3 months.
That's a bit complicated.
Here's a SQL Fiddle as a demonstration: http://sqlfiddle.com/#!2/c90e9/3.
For testing, NOW() is inconvenient because it always returns today's date. So, for testing, we replace all occurrences of NOW() with a user-defined variable #now, and set that to various dates, so we can appropriately test.
Here's the SQL statement I used for testing. The first expression is the conditional test we're planning on using in the WHERE clause. For testing, we want to return all the rows, and just see which rows get due_in_3mo flagged as TRUE (1) and which get flagged as FALSE (0).
The second expression in the SELECT list just the "next due" deadline date, same as used in the first expression.
The rest of the expressions are pretty self-explanatory... we also want to display the date 3 months in the future we're comparing to, and the original "deadline" date value.
SELECT
CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))
+ INTERVAL CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))<#now YEAR
< #now + INTERVAL 3 MONTH
AS `due_in_3mo`
, CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))
+ INTERVAL CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))<#now YEAR
AS `next_due`
, d.id
, d.deadline + INTERVAL 0 DAY AS `deadline`
, #now + INTERVAL 3 MONTH AS `now+3mo`
, #now + INTERVAL 0 DAY AS `now`
FROM d d
CROSS
JOIN (SELECT #now := '2015-11-01') i
ORDER BY d.id
Change the value assigned to #now in the inline view (aliased as i) to test with other date values.
(You may want to use DATE(NOW()) in place of NOW() so that times don't get mixed in, and you may want to subtract another day from that, that really just depends how you want to handle the edge case of a deadline with month and day the same as the current date. (i.e. do you want to handle that as "in the past" or not.)
To summarize the approach: generate the "next due" deadline date as a DATE value in the future, and compare to the a date 3 months from now.
I have a table of users with various dates like birthday, date of hire, and annual review date. The birthday may or may not have a year, the date of hire does have a year, and the review date does not have a year.
I want to be return a query across a date range that returns the rows with the dates calculated for the year of the date range for these recurring events.
For example:
A user with the following:
dob:1980-05-05
doh:2005-06-22
review:0000-10-01
Then if I query for a range of say, 2012-05-01 to 2013-12-01 I'd like it to return records like:
Event,Date
dob,2012-05-05
doh,2012-06-22
review,2012-10-01
dob,2013-05-05
doh,2013-06-22
review:2013-10-01
I realize that this probably will be done in separate UNION queries on each date field which is fine. This isn't exactly the same problem as finding dates x days in the future as I need the recurring date with the proper year in the given date range which may be over multiple years if the range is large enough.
I can only think of doing this programatically. First checking if the start and end range span over a year. Then split the dates into start date to end of year, start of year to end date etc. and run a query for each span.
i have day, month, year, value columns in one table, here i need to get every week average value in one month.
how to get that. please help me regarding this.
select avg(value) from table group by month
gives month average.
select avg(value) from table group by day
gives day average.
but how to get week average from month field.
You can't "get weeks from a month" as one is not a subset of the other.
The number of days (and hence weeks) in 1 month varies from month to month and only in non-leap years - and in February only - are there exactly 4 weeks in a month.
You should use the original date field and use a date function to limit/group the data by week.
get the week in mySQL with
WEEK(timestamp)
or
YEARWEEK(timestamp)
or
WEEKOFYEAR(NOW())
or
DATE_FORMAT($yourDate, \'%X %V\') as week
There might be a better way, but my first thought is to write a query that calculates the week of the year for each day and groups them.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekofyear
Something like:
SELECT avg(value) FROM table GROUP BY WEEKOFYEAR(CONCAT(year, '-', month, '-', day))