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())
Related
I'm trying to perform some time calculations (timestampadd, timestampdiff) on a query but I'm stuck with an unexpected behavior.
I executed in mysql this query:
select timestampdiff(MONTH, timestampadd(MONTH, 1, '2017-01-30'),'2017-01-30')
Using logic is adding to 2017-01-30 one month, then it requests the difference in months between this date and again 2017-01-30.
I'm expecting the result to be trivial and equal to 1 but instead the previous query evaluates to 0.
This screws my calculations.
Why is that?
This is straight forward,
you are adding 1 month in january 30 which will be feb 28 as in 2017
now the difference between jan30 and feb28 is only 29 days which is less than a month value. ( 30 days )
Therefore its 0
for accuracy, you need to handle february with care .
This question already has answers here:
How do I subtract using SQL in MYSQL between two date time values and retrieve the result in minutes or second?
(5 answers)
MySql difference between two timestamps in days?
(8 answers)
Closed 4 years ago.
SELECT (CURRENT_DATE - P.DataDiPrestito) AS "Sisssssss"
FROM prestito P, utente U
WHERE P.id_Utente = U.ID_Utente
So I'm subtracting by the current date the value of a date from my database to see the difference between them in days.
The problem is that, when I subtract a value that's in the same month the result is correct but when it's from a month before it's not.
Today is the 15 of May, and if subract from it the same date value I get 0, if subtract from it the date value of the day before I get 1 and so on and so forth.
But when I subract from a value from from April I get the correct value plus 70. Like, if I do (15 of May - the 30 of April) I should be getting 15 but instead I get 85 and so on, and if try to use March or February I get even higher values, if try (15 of May - the 6 of February) I get 309.
What can I do?
Subtracting dates from each other doesn't work like you think it does.
If you subtract the 30th of April from the 15th of May, this is what gets calculated internally:
20180515 - 20180430 = 85
The numbers are simply being treated like decimals.
The function you are looking for is called DATEDIFF().
You should check out the DATEDIFF() function. This will do what you want, giving the day difference between two dates.
DATEDIFF(expr1,expr2)
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)
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)
My db is made of groups of entries (by user) with a row for each day of the week and also groups where there is only 1 row per week of the year. This week may start Sat, Sun or Mon.
The sql groups all these rows by user id and works fine for the entries where the user has a row for every day
The problem I have is selecting the users rows where there is only one entry per week
Basically if the rows date is 11th Feb 2012 then I need to be able to select that row if the start date criteria falls on that date or within that following week and all rows upto but not including the row where the date column is after the end date
I'm trying everything like dateadd in the sql but I just cannot get it to add these rows in.
Hope I've made myself clear.
Say I have two entries in the db
2013-02-02
2013-02-09
I have a start date of 2013-02-05 and an end date of 2012-02-13
I need to get those two row as:
the start date falls on or within the week of 2013-02-02
and I also need 2013-02-09 as the end date falls on or within the week of that date.
Hope that makes it a bit clearer.
Not sure exactly what your question is asking.
If your field is of mysql date or datetime type, and you wanted to find if there was an entry for a given week could you not use MySQL WEEK Function to find all entries for the given week, you may also need to include a restriction on YEAR too.
You could also include the following week, but you may encounter problems. The main problem being week 52+1 of 2012 wont give week 1 of 2013, but week 1 of 2012.