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
 General SQL Server Forums
 New to SQL Server Programming
 Previous Months

Author  Topic 

lbunch
Starting Member

9 Posts

Posted - 2007-03-19 : 10:27:03
I have a View there I need my criteria to select 13 months from previous month.

Example. This is 3/1/2007 - I need to query 2/1/2007 to 2/1/2006.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-19 : 10:30:20
[code]
select dateadd(month, datediff(month, 0, getdate()) - 13, 0),
dateadd(month, datediff(month, 0, getdate()) - 1, 0)
[/code]


KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 10:30:43
[code]SELECT DATEADD(MONTH, -13, '20070301')
DATEADD(MONTH, -1, '20070301')[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

lbunch
Starting Member

9 Posts

Posted - 2007-03-19 : 11:15:04
I am going to use this statement but need to subtract 1 from current month. Since this is March I need only 13 months from February...

select dateadd(month, datediff(month, 0, getdate()) - 13, 0),
dateadd(month, datediff(month, 0, getdate()), 0)



Go to Top of Page

lbunch
Starting Member

9 Posts

Posted - 2007-03-19 : 11:23:07
I am testing the sql, but I need to show the dates in the DropDt where I have selected dates be the DropDt for 13 months prior to previous month (not current month).....

Thanks
Go to Top of Page

lbunch
Starting Member

9 Posts

Posted - 2007-03-19 : 11:48:56
Thanks KH - I figured to add -1 but where do I place the column name DropDt in this criteria so that the dates are filtered from that column. As it is now I get two different columns showing the dates but I am want to filter dates from the DropDt column....... I am confused.

SELECT dateadd(month, datediff(month, 0, getdate()) - 13, 0)
dateadd(month, datediff(month, 0, getdate()) - 1, 0)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-19 : 11:53:37
[code]
WHERE DropDt >= dateadd(month, datediff(month, 0, getdate()) - 13, 0)
AND DropDt <= dateadd(month, datediff(month, 0, getdate()) - 1, 0)
[/code]


KH

Go to Top of Page

lbunch
Starting Member

9 Posts

Posted - 2007-03-19 : 11:56:40
Thanks
Go to Top of Page
   

- Advertisement -