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

Date Function Query

Status
Not open for further replies.

kezzab

Programmer
Nov 9, 2005
14
GB
Does anyone know how I can use the date functions within SQL to return the rows where a specific date value is "any time in the previous calendar month"

e.g. if it was run now it would return everything from 01/08/06 to 31/08/06 inclusive.

Thanks in advance

 
Code:
SELECT *
FROM MyTable
WHERE DATEDIFF( MONTH, col_of_dates, getdate() ) = 1
 
Code:
Select *
From   MyTable
Where  DateField >= DateAdd(Month, DateDiff(Month, 0, GetDate())-1, 0)
       And DateField < DateAdd(Month, DateDiff(Month, 0, GetDate()), 0)

If run today, this query will return records where the date field is greater than (or equal to) [!]1 Aug 2006[/!] and Less then [!]1 Sep 2006[/!].

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top