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 2008 Forums
 Transact-SQL (2008)
 How to get date [month] before last date

Author  Topic 

hardikkumar
Starting Member

14 Posts

Posted - 2014-05-11 : 19:32:47
I have date column and based on which I have to get records till a month before last date.
I tried DATEADD but I am not getting Date before a Month for Date column records.

Can someone help?

Thanks.

hardik

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2014-05-11 : 20:47:13
Could you please explain the keyword on your post?

till a month before last date?

Give me some scenario. i will post the query
Go to Top of Page

hardikkumar
Starting Member

14 Posts

Posted - 2014-05-11 : 21:10:25
quote:
Originally posted by sqllover

Could you please explain the keyword on your post?

till a month before last date?

Give me some scenario. i will post the query



Hi,

I have to pull records from table for a month before date from last date.

i.e. if last date in ActivityDate column is 5/10/2014 then I have to pull records till 04/10/2014. A month should be deducted based on how many days that last month has.

I hope that explains.

and I appreciate your help.

Thanks.


hardik
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-05-11 : 21:53:26
[code]
; with dates as
(
select fr_date = dateadd(month, -1, dateadd(day, datediff(day, 0, max(ActivityDate))),
to_date = dateadd(day, -1, dateadd(day, datediff(day, 0, max(ActivityDate)))
from table
)
select *
from table
cross join dates
where ActivityDate >= fr_date
and ActivityDate < to_date
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2014-05-11 : 22:00:16
Here you go :

declare @ActivateDate datetime = '5/10/2014'

select dateadd(M, -1, getdate())


Have your datecolumn <= dateadd(M, -1, getdate())

Go to Top of Page
   

- Advertisement -