Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Last month of data

Author  Topic 

kweegly
Starting Member

7 Posts

Posted - 2013-01-15 : 06:10:05
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

52326 Posts

Posted - 2013-01-15 : 06:34:35
[code]
WHERE IssueDate >= dateadd(mm,datediff(mm,0,getdate())-1,0)
and IssueDate < dateadd(mm,datediff(mm,0,getdate()),0)
[/code]


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

Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2013-01-15 : 06:34:45
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

52326 Posts

Posted - 2013-01-15 : 06:37:28


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
   

- Advertisement -