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

Date function problem

Status
Not open for further replies.

mdr

MIS
May 14, 2003
17
0
0
GB
I want to get sales from the past three months.
I use a variable to get todays date:

declare @Today datetime
set @Today=getdate()

and then try and pass that through to this formula:

and datetable.datevalue between @Today and month(dateadd(m,-3, @today))

However, i am not getting any results.

Can anyone help?
 
Looks like you just need to flip the dates...

datetable.datevalue between month(dateadd(m,-3, @today)) and @today
 
Yep, the range you set for the BETWEEN keyword must go from lowest to highest.

--James
 
And why are you changing todays date (getdate()) into a variable? It's not necessary, this will work:

datetable.datevalue between month(dateadd(m,-3, getdate())) and getdate()

-SQLBill
 
Actually, there is an error in your expressions. Using the month function will return a simple integer value, so you are basically saying:

Code:
WHERE datevalue BETWEEN 3 AND '9th June 2003'

...which is not going to work!

What are you trying to achieve with the MONTH function?

--James
 
Use this instead:

datetable.datevalue between dateadd(mm,-3, getdate()) and getdate()

I ran:

select dateadd(mm,-3,getdate())
and it returned:

2003-03-09 11:56:51.663

-SQLBill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top