IanWaterman
Programmer
Hi
Using 2008
I need to write a query which is to be run at the beginning of each month and bring back records from previous 12 month.
ie if I run any time during March 2010, it should select records with dates in range
1 March 2009 - 28 Feb 2010, catering for leap years too.
when I run in April 2010 must bring back 1st April 2009 to 31 March 2010 and so on.
I know I can use Datediff to get me back one year but can not fathom how to then derive the 1st of that month?.
Also need logic to find last day of previous month. I can do this with Crystal logic but do not know enough MS SQL to replicate.
Its not guaranteed to run on Ist of month so I can't use getdate() - 1 to determine last day of previous month.
Thank you
Ian
Using 2008
I need to write a query which is to be run at the beginning of each month and bring back records from previous 12 month.
ie if I run any time during March 2010, it should select records with dates in range
1 March 2009 - 28 Feb 2010, catering for leap years too.
when I run in April 2010 must bring back 1st April 2009 to 31 March 2010 and so on.
I know I can use Datediff to get me back one year but can not fathom how to then derive the 1st of that month?.
Also need logic to find last day of previous month. I can do this with Crystal logic but do not know enough MS SQL to replicate.
Its not guaranteed to run on Ist of month so I can't use getdate() - 1 to determine last day of previous month.
Thank you
Ian