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
 Question about date and reports

Author  Topic 

mclovin
Starting Member

15 Posts

Posted - 2012-12-03 : 13:09:28
Hello , here is my question, i would like to run a job that creates a report every month. This report will be run on the 15th of every month. What i need is the report to show from the previous month of the 15th to the current month 14th. I know I have to use a datediff and probably a dateadd but not to sure how to go about it. Any help would be appreciated thanks.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-03 : 14:24:40
Use a where clause like this:
WHERE 
YourDateColumn >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,14)
AND YourDateColumn < DATEADD(mm,DATEDIFF(mm,0,GETDATE()),14)
This is really asking to start on the 15th of last month and go up to anything that is LESS than the 15th of the current month - which is the best way to do what you described.
Go to Top of Page

mclovin
Starting Member

15 Posts

Posted - 2012-12-03 : 14:33:14
Thanks sunitabeck for Responding, I'll give that a try.
Go to Top of Page

mclovin
Starting Member

15 Posts

Posted - 2012-12-03 : 15:50:06
Thanks again sunitabeck it worked just like I needed it too
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-04 : 01:09:02
see this to understand the logic used

http://visakhm.blogspot.in/2012/07/generate-datetime-values-from-integers.html

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

Go to Top of Page
   

- Advertisement -