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 2005 Forums
 Transact-SQL (2005)
 Need a current month query in sql 2005

Author  Topic 

omega1983
Starting Member

40 Posts

Posted - 2009-11-20 : 13:21:45
I have a portion of code:

select ID,Name
where gifteffdat >='2009-10-01' and gifteffdat <='2009-10-30'
from gifts

Now I need a way of showing records each month as long as thet fall within a calendar month. I need to get away from hardcoding the month range

I need something like if the gifteffdat falls within the first and last day of the currentmonth, show the records

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-11-20 : 13:58:51
[code]
where datediff(month, gifteffdat, getdate()) = 0 --current month
where datediff(month, gifteffdat, getdate()) = 1 --previous month
[/code]

Be One with the Optimizer
TG
Go to Top of Page

omega1983
Starting Member

40 Posts

Posted - 2009-11-20 : 14:23:33
Thank you very much it worked
quote:
Originally posted by TG


where datediff(month, gifteffdat, getdate()) = 0 --current month
where datediff(month, gifteffdat, getdate()) = 1 --previous month


Be One with the Optimizer
TG

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-20 : 14:35:34
Yes and it will do a table or index scan



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-11-20 : 15:21:06
quote:
Originally posted by X002548

Yes and it will do a table or index scan

Brett


That's true. If the column has an index this may be better:

--current month
where gifteffdat > dateadd(month, datediff(month, 0, getdate()), 0)
and gifteffdat <= dateadd(month, datediff(month, 0, getdate()), 1)


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -