Solved

SQL Max and Min date for prev month

Posted on 2012-03-12
14
426 Views
Last Modified: 2012-03-16
Hi experts.

Not sure is this is possible but im creating a view to pull data for the previous month. I need the WHERE clause to have the Min date of the first day of perv month and the Max date to be last day of the previous month.

Example:

Current Date:  2012-03-12

WHERE Date Between '2012-02-01 AND '2012-02-29'

Thanks,
Dean
0
Comment
Question by:deanlee17
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 37710620
try

where date between
 dateadd(month, -1,
   dateadd(dd, -1 * datepart(dd, getdate()) + 1, getdate())
)
and
   dateadd(dd, -1 * datepart(dd, getdate()) , getdate())
0
 
LVL 35

Expert Comment

by:YZlat
ID: 37710980
WHERE MyDate BETWEEN DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))-1),DATEADD(mm,-1,GETDATE()))
AND DATEADD(dd, -DAY(DATEADD(m,1,GETDATE())), DATEADD(m,0,GETDATE()))
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37712803
If you do not want the time in the condition and you are not using SQL Server 2008 then consider this small variation (no points please):

DECLARE @Today smalldatetime
SET @Today = DATEADD(day, 0, DATEDIFF(DAY, 0, GETDATE()))

...

WHERE Date BETWEN DATEADD(day, 1 - DAY(DATEADD(MONTH, -1, @Today)), DATEADD(MONTH, -1, @Today)) AND DATEADD(DAY, -DAY(@Today), @Today)

Open in new window

0
 

Author Comment

by:deanlee17
ID: 37718730
acperkins: Could you explain exactly how... these lines work....

= DATEADD(day, 0, DATEDIFF(DAY, 0, GETDATE()))

BETWEN DATEADD(day, 1 - DAY(DATEADD(MONTH, -1, @Today)), DATEADD(MONTH, -1, @Today)) AND DATEADD(DAY, -DAY(@Today), @Today)

Thanks.
0
 

Author Comment

by:deanlee17
ID: 37718779
momi_sabag:

This worked perfectly. Can you explain exactly how is works?

Thanks,
Dean.
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 37720169
sure
dateadd(dd, -1 * datepart(dd, getdate()) + 1,  -- this returns the first day of the current month by subtracting the number of days that have passed since the month started

wrapping it with
 dateadd(month, -1,
 getdate())
)
will return the first day of last month

and
   dateadd(dd, -1 * datepart(dd, getdate()) , getdate()) -- this returns the last day of the previous month
0
 

Author Comment

by:deanlee17
ID: 37720232
momi_sabag, ive found a problem, it doesnt bring back results for 1st Feb only 2nd feb onwards?!

Thanks,
Dean
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Expert Comment

by:momi_sabag
ID: 37720269
what exactly did you execute?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37721621
it doesnt bring back results for 1st Feb only 2nd feb onwards?!
Now hopefully you see what I meant by "If you do not want the time in the condition"
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37721634
Could you explain exactly how... these lines work....
See if this helps:
DECLARE @Today smalldatetime
SET @Today = DATEADD(day, 0, DATEDIFF(DAY, 0, GETDATE()))

SELECT	DATEADD(day, 1 - DAY(DATEADD(MONTH, -1, @Today)), DATEADD(MONTH, -1, @Today)), 
	DATEADD(DAY, -DAY(@Today), @Today)

Open in new window


Incidentally, there was a typo in the solution i posted earlier this:
WHERE Date BETWEN DATEADD(day, 1 - DAY(DATEADD(MONTH, -1, @Today)), DATEADD(MONTH, -1, @Today)) AND DATEADD(DAY, -DAY(@Today), @Today)

Should have read:
WHERE Date BETWEEN DATEADD(day, 1 - DAY(DATEADD(MONTH, -1, @Today)), DATEADD(MONTH, -1, @Today)) AND DATEADD(DAY, -DAY(@Today), @Today)
0
 

Author Comment

by:deanlee17
ID: 37722312
Hi acperkins

Thanks for the reply, but I am using SQL Server 2008, will this cause a problem?

Thanks,
Dean.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 37722373
No, but with SQL Server 2008 you can use the new date data type and just do:

WHERE [Date] BETWEEN DATEADD(day, 1 - DAY(DATEADD(MONTH, -1, CAST(GETDATE() AS date))), DATEADD(MONTH, -1,CAST(GETDATE() AS date)))
      AND DATEADD(DAY, -DAY(CAST(GETDATE() AS date)), CAST(GETDATE() AS date))
0
 

Author Comment

by:deanlee17
ID: 37723818
acperkins, that worked perfectly. Can you explain where why momi_sabag's method failed as opposed to yours?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37726057
>>Can you explain where why momi_sabag's method failed as opposed to yours?<<
As I mentioned previously, the only difference between the two is the time.  See for yourself:
SELECT dateadd(month, -1,   dateadd(dd, -1 * datepart(dd, getdate()) + 1, getdate())),
   dateadd(dd, -1 * datepart(dd, getdate()) , getdate())
   
SELECT DATEADD(day, 1 - DAY(DATEADD(MONTH, -1, CAST(GETDATE() AS date))), DATEADD(MONTH, -1,CAST(GETDATE() AS date))),
       DATEADD(DAY, -DAY(CAST(GETDATE() AS date)), CAST(GETDATE() AS date))

That tell me that your date column includes a time.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Viewers will learn how the fundamental information of how to create a table.

747 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

11 Experts available now in Live!

Get 1:1 Help Now