Query to return data for the past 6 months - ms-access

I'm having some troubles in trying to figure out a date criteria in a query. Basically, I'm trying to get all the data from the past 6 months. For example, the current month is December 2017; I would like the query to return data ranging from June to December 2017. I've tried the following criteria:
Between Date() and DateAdd ("M", -6, Date())
However, the criteria returns data based on the day of the date; if the current date is 2 December 2017, the query returns dates from 2 June to 2 December 2017. I want the query to return data from the whole month of June (therefore if we're following the previous example, data from 1 June 2017 will be included too) to December. How do I go about achieving this?

The problem you're having sounds quite simple. You really just need to use the 1st day of the current month, instead of the current date.
There are many ways to get the first day of the current month, for example:
Date() - Day(Date()) + 1
While there are alternate ways to do this, try not to rely on casting a date to a string and back for performance/locale incompatibility
If you use this approach, your SQL WHERE would be:
Between Date() - Day(Date()) + 1 and DateAdd ("M", -6, Date() - Day(Date()) + 1)

Related

Date Intervals Doesn't work

I am trying to get data by week, month and year.
I store date YYYY-MM-DD HH:MM:SS.
What I am doing is below;
Fetch one week old data;
query + AND WEEK(date) = WEEK(CURDATE())
Fetch a month old data;
query + AND MONTH(date) = MONTH(CURDATE())
The thing is I couldnt be able to get the data correct. For instance when I want to get week old data, I am gettin a year old one too.
Is there any other query that I could use? I have tried DATE(NOW()) - INTERVAL 30 DAY. It works but very slow.
Thanks!
I believe that the problem is that the WEEK function returns the week of the year. So, Jan 1st 2017 might be week 1 (also might be week 53 of the previous year depending on the day of the week and how MySQL handles it). But then, Jan 1st of 2016 is also week 1 - just for a different year.
Trying changing it to:
query + AND WEEK(date) = WEEK(CURDATE()) AND YEAR(date) = YEAR(CURDATE())
Also, if you're storing this as a string then definitely change it to a DATETIME
WHERE ...
AND date >= CURDATE() - INTERVAL 7 DAY
AND date < CURDATE()
Gives you the 7 days ending with yesterday. Use other techniques to get a particular month or week.
This technique is also much faster for large tables with a suitable index. Hiding date inside a function, such as WEEK() prevents the use of an index.

date calculation with full date in separate columns

I'm trying to make a query for the last 3 months of an item with my month and year in separate columns like so:
YEAR_ PERIOD
2014 5
2013 6
2013 11
2011 6
2009 2
The query needs to always start from the current month and year. I've tried using DateAdd(), DateSerial(), and DateDiff() none of those worked. Whenever I try to use month(now()-3) i'm getting 2 instead of 11.
Adding or subtracting integers and dates simply adds or subtracts days from the date. So Now() - 3 results in 2016-02-15 (it is 2016-02-18 at the time of this posting). That is clearly still the month of February - hence your result of two.
Give this a try Month(DateAdd("m", -3, Now)). Here we are adding -3 months to the current date and then getting the resulting month. Based on today's date that will result in 11.
I figured it out.
DateDiff("m",CDate(Format([PERIOD] & "/" & [YEAR_],"mm/yyyy")),Now())
This took the two fields and made them a single date. I then took the difference from this month and the months between the two dates. I then set the criteria to <= 3.
Addendum
It can be simplified to:
DateDiff("m",CDate([PERIOD] & "/" & [YEAR_]),Date())
In general, however, you should never use string handling for dates if it can be avoided, and it easily can:
DateDiff("m",DateSerial([YEAR_],[PERIOD],1)),Date())

Access Criteria currently Month and next Month

I need to access criteria Currently Month and Next Month
For example : For Today i need 01.04.15 to 31.05.15 but next Month ill need 01.05.15 to 31.06.15
I wrote to criteria this code but i could not get any solution
**
BETWEEN DateSerial(Year(Date()); Month(Date()); 1) AND
DateSerial(Year(Date()); Month(Date()) + 2; 0)
**
if you have a solution about this problem. Please Share with me
Thanks
Y.Ö.
Between DateSerial(Year(Date()); Month(Date()); 1) And DateSerial(Year(Date()); Month(Date()) + 2; 0)
is correct, so perhaps you just don't have any data within this range.
Think about it. What happens when current month is Nov & Dec then month + 2 = 13 or 14 and not a valid month. Hence, you will need a IIf statement to handle the year being advanced to next year and to handle the Year and the month accordingly. Also, think of the Day again you will need a IIF statement to handle months ending 28, 30 and 31.
Sorry, I was wrong how Access handle the Yearend and the month. Here's some more info. that was posted on https://access-excel.tips/; So; I am not taking the credit.
Unlike first date of month, we cannot directly input “31” in the day argument because every month has different number of days. Instead we can find the first day of next month and then minus 1 day. Access is very clever that when you add 1 month to December, the year also adds 1, so this trick also works for year end. The formula below returns the last date of current month.
DateSerial(Year(Date()),Month(Date())+1,1)-1
We can also simply this formula using 0 in the day argument instead of using -1. 0 returns the last date of previous month.
DateSerial(Year(Date()), Month(Date()) + 1, 0)

Selecting rows with a set date+a month offset with MySQL

I have a MySQL database with one table that contains a data field and a "period" field, in months - int.
The idea is that the date indicates a due date to begin a project inside my company. And the "period" the period of time it is suppose to take to finish it, in months.
I need to select rows that will impact a given year. So if I am generating a report for 2014, I need to select the rows such: date+period is inside 2014.
It will be easy to do it inside the program, but I am looking for a way to do it in the query - if possible.
So basically I just need a way to sum dates and ints in a query, where the int is the number of months.
Any thoughts?
It's easy to do date arithmetic in MySQL and other RDMS systems. You need all the records in which the start date is not after the year in question OR the end date is not before the year in question. That is this expression:
NOT(YEAR(start_date) > 2014 OR YEAR(start_date + INTERVAL period MONTH) < 2014)
This logically reduces to
YEAR(start_date) <= 2014 AND YEAR(start_date + INTERVAL period MONTH) >= 2014
So this query will do it.
SELECT whatever, whatever
FROM project
WHERE YEAR(start_date) <= 2014
AND YEAR(start_date + INTERVAL period MONTH) >= 2014
AND (whatever other selection criteria you have)
This will give all projects that were active during 2014, including those that started before 2014 and those that will still be in progress at the end of that year.

How To Get YEARWEEK() To See Sunday As The Start Of The Week?

I'm trying to get the YEARWEEK function to see Sunday as the 1st day of the week.
An example date is: Sunday 1st Mar 2009
Here is my sql
SELECT YEARWEEK('2009-03-01')
and the result is
-> 200909
Which is week 9. I believe it is telling me this is week 9 because it sees the sunday as the last day of the previous week.
How would I get this function to see sunday as the 1st day of the week and therefore return as week 10?
According to http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_yearweek, YEARWEEK has an optional second argument which controls this. YEARWEEK('2009-03-01', 0) should do what you want (but see the table of possible values under WEEK on that page).
this will do it:
SELECT YEARWEEK('2009-03-01')+IF(WEEKDAY('2009-03-01')=6,1,0);
EDIT: this is the better solution, for more information click here:
SELECT YEARWEEK('2009-03-01',0);
(but i don't know why you want to do this - 2009.03.01 is in week 9, not 10...)