Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Current Month vs hard coding the month in SQL

Status
Not open for further replies.

sap1958

Technical User
Oct 22, 2009
138
US
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
 
how about
select ID,Name
from gifts
where month(gifteffdat)=month(Getdate())
And Year(gifteffdat)=Year(Getdate())

 
Code:
 SELECT ID,Name
   FROM gifts
  WHERE gifteffdat >= DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
    AND gifteffdat <  DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0)

--Jeff Moden
-------------------------------------------------------------------------------
"RBAR" is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top