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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query on Month to date?

Status
Not open for further replies.

PQTIII

IS-IT--Management
Sep 21, 2004
110
What is the correct where statement to show my all the records in the current month or month to date?

Thanks pt
 
Assuming that your date field is called LastUpdated you could write:

Select * Where DATEDIFF(D, LastUPDate, GETDATE()) <= DAY(GETDATE()-1)

This calls the DATEDIFF (Date Difference) Function
states that you are calculating by (D, Days)
LastUpdate is the oldest date
GetDate() is today
and that the calculation is <= Today's date - 1 day

Good Luck

CPT Tom
 
SELECT *
FROM MyTable
WHERE DatePart(mm,MyDate) = DatePart(mm,GetDate())
AND DatePart(year,MyDate) = DatePart(year,GetDate())

Thanks

J. Kusch
 
Some other variations:
Code:
-- first
select *
from myTable
where year(datecolumn) = year(getdate()) and month(datecolumn) = month(getdate())

-- second
select *
from myTable
where datediff(mm, datecolumn, getdate()) = 0

-- third
declare @lodate datetime; set @lodate = dateadd(mm, datediff(mm, 0, getdate()), 0)
declare @hidate datetime; set @hidate = dateadd(mm, 1, @lodate)

select *
from myTable
where datecolumn >= @lodate and datecolumn < @hidate


------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top