Solved

Need SQL code to query month to date last year

Posted on 2014-10-29
4
642 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now