Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need date code to pull "last month" month to date

Posted on 2014-04-23
5
Medium Priority
?
792 Views
Last Modified: 2014-04-25
to query month to date data I  am using  

Between DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

Today being 4/23 it gives me everything between 4/1 and 4/22. Now I need a piece of code that gives me last month month to date if that makes sense, It would need to pull 3/1 to 3/22. As the month progresses it would then of course add the previous day to the queried results like the

Between DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

does for the current month. I am using this for creating compairable time frame sales data. Thanks
0
Comment
Question by:Cole100
[X]
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
5 Comments
 
LVL 10

Expert Comment

by:Korbus
ID: 40018573
>> DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

You are so close! (I'm putting parts of your original statement in bold)


DateAdd("m",-1,DateAdd("d",-DatePart("d",Date())+1,Date())) And DateAdd("m",-1,Date()-1)

(P.S. sorry I cannot test this for syntax errors right now)
0
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 100 total points
ID: 40018623
If you need this as two columns within a single query, you might use something like:

SELECT
SUM(iif([DateField] >= Dateserial(Year(date()), Month(Date()), 1) AND
              [DateField] < Date(), [Amount], 0)) as ThisMonthToDate,
SUM(iif([DateField] >= Dateserial(Year(date()), Month(Date())-1, 1) AND
              [DateField] < DateSerial(Year(Date()), Month(Date()) - 1, Day(Date()), [Amount], 0)) as LastMonthToDate
From yourTable
WHERE yourTable.[DateField] >= DateSerial(Year(Date()), Month(Date()) - 1, 1)

This would give you the sum of the [Amount] field in YourTable for the two time periods.
You could add columns and group by those columns if you needed, for example the sales by product over the same period in successive periods.
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 100 total points
ID: 40018627
Don't mess with the days:

Between DateSerial(year(Date()), Month(Date()) - 1, 1) And DateAdd("m", -1, Date())

/gustav
0
 

Author Closing Comment

by:Cole100
ID: 40021415
Gustav, yours worked exactly how I was asking for it. Thank you

Fyed, that does what I needed and more. Thank you
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40021987
You are welcome!

/gustav
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

722 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