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)
Related
I try to find a function where I can extract the result of the last month only
(for exemple if I launch the query in november, I want to display only the resultat of october)
There the result :
I dont know if I have to enter the function in my select or where clause
Thanks for you help!!
CHeers!
I tried the function month(date, -1)
I want to see all the result for the previous month
You can try getting the date and applying in the where clause.
Note that there may be more efficient options available.
Query to Use:
WHERE
DATE_TRANSACTION BETWEEN
trunc(date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)),'MM')
AND
date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE))
Explanation:
CURRENT_DATE - Gives the current Date
dayofmonth(CURRENT_DATE) - Gives the day part of current date.
date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)) - Gives the last day of previous month. Assume current_date is 2022-11-23, dayofmonth will give 23, when you subtract 23 days, it goes to the last day of previous month i.e. 2022-10-31.
trunc(date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)),'MM') - Truncates date to first day of month.
DATE_TRANSACTION - between these two days.
I have a portion of a WHERE clause below and I though I had this right but I dont seem to:
AND (WEEK(DeliveryDate, 1) = (WEEK(CURDATE(), 0) - 1))
AND (YEAR(DeliveryDate) = YEAR(CURDATE())))
The way I though this worked according to the documention was that the "1" here (WEEK(DeliveryDate, 1)' was telling it to start the week on Monday. Then the "0" here = (WEEK(CURDATE(), 0) - 1)) was telling it to start in the week that just ended. This is however not what is happening. Instead this expression is calculating the week the ended November 29th instead of this past week that ended December 6th. What is being missed?
EDIT 1
The above WHERE clause is what I was told to move to at one point at the suggestion of another user. I had previously been using CURDATE() then the Interval between X and Y to get one week ago, not including this week and not the last 7 days. Here is the result of the folling query:
select post_id,DeliveryDate from table where deliverydate >= now() - interval 1 week
If you are trying to see if DeliveryDate is in the previous week, where weeks start on Monday, do:
AND YEARWEEK(DeliveryDate, 1) = YEARWEEK(CURDATE() - INTERVAL 1 WEEK, 1)
Adjusting the return of week won't work well across year boundaries, and checking week and year separately can go wrong in a number of ways, and comparing week values using different modes isn't going to do anything useful.
I am creating a package which will store a report's generation date. on basis of that date need to derive the next Wwednesday date.
Ex: report date is 11/11/2019 so wed date should be 13/11/2019.
This Derived Column Expression should provide the next Wednesday's date.
DATEADD("DAY",((1 + DATEDIFF("DAY",(DT_DATE)"1/1/1970",GETDATE())) / 7) * 7 + 6,(DT_DATE)"1/1/1970")
Parts (inside out).
Days since 1/1/1970 + 1.
(Divide by 7) (Implicit Cast to Int) (Multiple by 7) will round it to last Thursday.
When added days back to 1/1/1970 (+6 days more) will move to next occurring Wednesday.
Notes
Without the initial 1 day offset, If GETDATE() is a Wednesday, the result will be GETDATE()'s date.
GETDATE() can be swapped out with GETUTCDATE() or a date Variable as needed.
The +6 can be moved back to +5 if a Tuesday is desired.
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'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())