Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Need SQL code to query month to date last year

Posted on 2014-10-29
4
669 Views
Last Modified: 2014-10-30
I am trying to build a query in Access 2010 to pull data from month to date last year.

Example. today being 10/29/14, I am using Between DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1 which pulls my data for 10/1/2014 to 10/28/2014

I am trying to look a comparable sales data so I need a piece of code that pulls 10/1/2013 to 10/28/2013
0
Comment
Question by:Cole100
4 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40411906
Did you try this:

dateadd("y",-1,DateAdd("d",-DatePart("d",Date())+1,Date()) ) and dateadd("y",-1,Date()-1)
0
 
LVL 19

Accepted Solution

by:
Eric Sherman earned 250 total points
ID: 40412006
Also try this for month to date last year ...

Between DateSerial(Year(Date())-1, Month(Date()), 1) AND DateSerial(Year(Date())-1, Month(Date()), Day(Date())-1)

ET
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 250 total points
ID: 40412634
You could use for the current year:

    Between DateSerial(Year(Date()), Month(Date()), 1) And DateAdd("d", -1,Date())

and for last year:

     Between DateSerial(Year(Date()) - 1, Month(Date()), 1) And DateAdd("yyyy", -1, DateAdd("d", -1,Date()))

etsherman's expression for the last year is valid too. A matter of taste.

/gustav
0
 

Author Closing Comment

by:Cole100
ID: 40413926
Both etsherman and gustav solution did exactly as i  needed. thank you
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question