SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Last month of data
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kweegly
Starting Member

7 Posts

Posted - 01/15/2013 :  06:10:05  Show Profile  Reply with Quote
Hi Everyone,

I have a field called IssueDate and would like to pull IssueDates from only the previous month but I can't seem to get it working.

I use BETWEEN GETDATE() -7 AND GETDATE() to get the last 7 days but I don't know how to get the last month (not last rolling 30 days).

Thanks for any help in advance.

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/15/2013 :  06:34:35  Show Profile  Reply with Quote

WHERE IssueDate >= dateadd(mm,datediff(mm,0,getdate())-1,0)
and IssueDate < dateadd(mm,datediff(mm,0,getdate()),0)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ashishashish
Constraint Violating Yak Guru

India
408 Posts

Posted - 01/15/2013 :  06:34:45  Show Profile  Send ashishashish a Yahoo! Message  Reply with Quote
Here is an bad method to get that data. Put that in where condition.


WHERE DATEPART(MONTH,IssueDates) = CASE WHEN DATEPART(MONTH,GETDATE())-1 = 0 THEN 12 ELSE DATEPART(MONTH,GETDATE())-1
AND DATEPART(YEAR,IssueDates) = CASE WHEN DATEPART(MONTH,GETDATE())-1 = 0 THEN 12 ELSE DATEPART(YEAR,GETDATE())-1

------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/15/2013 :  06:37:28  Show Profile  Reply with Quote


see

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html

to understand the logic involved

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000