Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL going back to start and end of month for 2 months ago

Posted on 2014-03-13
8
Medium Priority
?
435 Views
Last Modified: 2014-03-13
I need to build out 2 parameters for a start and end date that goes back 2 months.  In this example, it is currently March so my start date will be 1/1/2014 and my end date will be 1/31/14.

I got the start date with this:   =DateAdd("m", -2, DateSerial(Year(Now()), Month(Now()), 1))

If I use this for the end date:  =DateAdd("d", -1, DateSerial(Year(Now()), Month(Now()), 1))
 it obviously brings in 2/28/14 instead of 1/31/14.  I'm not sure how to combine the 2 for my end date so it becomes 1/31/14.
0
Comment
Question by:cindyfiller
[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
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39927560
Use another dateadd (month,-1, .... ) to achieve that result
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39927573
fyi SQL Server 2012 introduces the EOMONTH function, which will return the end of the month for any passed day.

SELECT EOMONTH(GETDATE()) when run now returns 2014-03-31.
0
 

Author Comment

by:cindyfiller
ID: 39927601
I was trying to do another dateadd, but wasn't doing it in the right spot - it was bombing out on me.  Something wasn't right!
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

Author Comment

by:cindyfiller
ID: 39927603
Unfortunately I'm still on 2008 eomonth won't work.
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 39927667
Maybe endDate =

 DateAdd("d", -1, DateAdd("m", 1, DateSerial(Year(Now()), Month(Now()), 1)))

?
0
 

Author Comment

by:cindyfiller
ID: 39927682
Thank you - that worked with one slight tweak.   I had to do -1 for DateAdd("m", -1...
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 39927772
D'OH, sorry, quite right, I misread the end date you were looking as being the end of the current month, not the previous month.
0
 

Author Comment

by:cindyfiller
ID: 39927792
It was easy to fix and your code worked, so I sincerely appreciate it!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

596 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