Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Shilabrow is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    May 2014
    Posts
    34

    Sql statement to retrieve prior month data using computer date as reference

    I need help with sql statement to retrieve prior month data while using the computer date as reference.

    select * from table
    w
    here date BETWEEN #05/01/2014# AND#05/31/2014#);


    Instead of putting the prior month date myself in query or using parameter, I need the query to use the computer day to retrieve prior month. So if we are in any day of the month of May, the query should see May and retrieve April 01 to April 30 data and so on for other month. Any help will be appreciated. Thanks


  2. #2
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    This will get you the current month.
    CurMonth: Format(DateSerial(Year(Date()),Month(Date()),Day(D ate())),"mmmm ")
    Previous Month:
    PrevMonth: Format(DateSerial(Year(Date()),Month(Date())-1,1),"mmmm")
    HTH

  3. #3
    Shilabrow is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    May 2014
    Posts
    34
    Thanks for the quick reply, I get data type mismatch in criteria expression when I used either of the two. I actually need to retrieve for previous month only but I get error. All help will be appreciated - Thanks

  4. #4
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I don't know why! Did you copy and paste as posted into your query grid?

  5. #5
    Shilabrow is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    May 2014
    Posts
    34
    Yes - for Previous month, I copied and pasted Format(DateSerial(Year(Date()),Month(Date())-1,1),"mmmm") in the criteria box. What am I doing wrong? So now I have

    SELECT [Purchase Orders].[Supplier ID], [Purchase Orders].[Creation Date], [Purchase Orders].[Shipping Fee], [Purchase Orders].TaxesFROM [Purchase Orders]
    WHERE ((([Purchase Orders].[Creation Date])=Format(DateSerial(Year(Date()),Month(Date()),Day (Date())),"mmmm")));

  6. #6
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Creation Date is not going to equal PrevMonth:Format(DateSerial(Year(Date()),Month(Date())-1,1),"mmmm")
    Your looking for the value of PrevMonth and use that as your criteria.


  7. #7
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    How about this:
    Code:
    [Purchase Orders].[Creation Date] BETWEEN DateSerial(Year(Date()),Month(Date())-1,1) AND DateSerial(Year(Date()),Month(Date()),0)
    For explenation: DateSerial([Year],[Month],0) will give you the last day of the previous month of [Month].

  8. #8
    Shilabrow is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    May 2014
    Posts
    34
    Thanks hapm. Works like a charm! I appreciate this and thanks for the explanation too.

    Burrina - Thanks for your help too!

  9. #9
    Shilabrow is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    May 2014
    Posts
    34
    Hapm, I would appreciate your helping me with this.

    I am trying to get data for 3months to date. I have the following but I get error when I used the below query for my date criteria. What am I doing wrong. All help appreciated in helping me retrieve all 3months data to date using computer reference time. Thanks

    DateDiff("m\on\th","datecolumn",getdate()<=3)

  10. #10
    axolotl66 is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    2
    Hi,
    I would use this:
    DATECOLUMN Between DateDiff("m", -3, Date()) and Date()
    Hth
    Graham

  11. #11
    Shilabrow is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    May 2014
    Posts
    34
    axolotl66, Thanks but I get a lot more data than needed, It retrieved about 2 year back record rather than just 3months back from today. Could it be because of the Date() at the end. All help appreciated.

  12. #12
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Simply change the condition to:
    Code:
    [Purchase Orders].[Creation Date] BETWEEN DateSerial(Year(Date()),Month(Date())-3,1) AND DateSerial(Year(Date()),Month(Date()),0)

  13. #13
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Or if you want to have it exactly on the current date, do what axolotl66 suggests but change DateDiff to DateAdd.

  14. #14
    Shilabrow is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    May 2014
    Posts
    34
    Thanks Hapm, I actually want 3month back data from daily date. (April 22 - June 24 todate) or 90 days. Your query worked by retrieving 3 month back to last month of MAy. So I get March, April and May to end data/records. axolot query with the DateAdd gave me error (datatype mismatch). Thanks for your help - so close hapm.
    Last edited by Shilabrow; 06-24-2014 at 01:17 PM.

  15. #15
    axolotl66 is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    2
    My bad, I meant to write dateadd not datediff! I am always doing that! That should add negative 3 months (the same as taking away) for the first argument of the between and then date() is today for the current date.
    Hapm's answer is going to be useful for me... Thanks!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 43
    Last Post: 08-06-2014, 08:21 PM
  2. Replies: 5
    Last Post: 02-07-2014, 09:53 AM
  3. Replies: 3
    Last Post: 08-18-2012, 03:25 AM
  4. Add one day to prior record's date/ how??
    By mkfloque in forum Access
    Replies: 3
    Last Post: 05-30-2012, 04:44 AM
  5. Date Parameter Help - prior 13 weeks
    By acw1980 in forum Access
    Replies: 1
    Last Post: 11-12-2009, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums