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 Function

Author  Topic 

diggydee
Starting Member

5 Posts

Posted - 2009-10-02 : 11:13:34
I have a job running 1st of every month to get last month data, I used this as date condition dateadd(m,datediff(m,0,getdate()-1),0) if I run today I have to change that -1 to -2 ….is that any date function available so that when ever I ran I have to get last month data

Any Hits are appriciated

Thanks
Diggy

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 11:16:16
yup just use

SELECT columns... FROM YourTable WHERE datefield>=DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0) AND datefield<DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-02 : 11:16:49
to get last month date range

use

where datecol >= dateadd(month, datediff(month, 0, getdate()) - 1, 0) -- 1st of last month
and datecol < dateadd(month, datediff(month, 0, getdate()), 0) -- 1st of current month



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

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-02 : 11:17:26


too slow


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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 11:19:26
quote:
Originally posted by khtan



too slow


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




Nope just a matter of 33 sec
Go to Top of Page

diggydee
Starting Member

5 Posts

Posted - 2009-10-02 : 11:29:18
Thanks a lot guys
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 11:31:36
welcome
Go to Top of Page
   

- Advertisement -