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
 Date Problem

Author  Topic 

mjimenezh
Yak Posting Veteran

81 Posts

Posted - 2014-04-03 : 17:25:43
Hi, I have a problem with a date in my sql view, I need the records from the 3 last months but without the current month, if I execute my view right now I have the records from January to april but I just need from January to march, I must have always the 3 previous month but I don't know how I can do it

(dbo.frhkrg.fakdat >= DATEADD(MM, - 3, GETDATE()))


Thanks for your help.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-04-03 : 18:02:47
I'm not 100% sure how you want to filter you data, but maybe this will help:
SELECT 
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) FirstOfCurrentMonth
,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 3, 0) FirstOfThreeMonthsAgo


SELECT
...
WHERE
dbo.frhkrg.fakdat >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 3, 0)
AND dbo.frhkrg.fakdat < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
Go to Top of Page

mjimenezh
Yak Posting Veteran

81 Posts

Posted - 2014-04-03 : 18:12:36
Thanks Lamprey, it works perfect.

God Bless You...
Go to Top of Page
   

- Advertisement -