Link to home
Start Free TrialLog in
Avatar of Tpaul_10
Tpaul_10Flag for United States of America

asked on

Need a query


Experts,

Need a query to get the results anything <= 90 days of the purchasedate from my values below.

=============
PurchaseDate
=============
2011-12-12
2011-01-01
2011-07-05
2011-07-03
2011-05-01
2013-01-01
2012-01-01

I am trying the following but it's not showing 2011-12-12 ,2013-01-01 ,2012-01-01 in my results set.

select * From mydb..mytable datediff(day,getDate(),purchasedate) <= (90)

Thanks in adance.
Avatar of dqmq
dqmq
Flag of United States of America image

Huh?  Because those are more than 90 days in the future????

What do you mean "anything <=90 days of the purchase date"
Avatar of _agx_
None of those are less than 90 days

Date                 datediff(day,getDate(),purchasedate)
2011-12-12      217
2012-01-01      237
2013-01-01      603
I'm not sure what you mean either....

I'll take a stab at it and guess you meant: "I want to find all purchases made within the last 90 days".  

        WHERE  purchasedate >= #ninetyDaysAgo#
        AND       purchasedate < #dateTomorrow#

ie ...
       WHERE  purchasedate >= <cfqueryparam value="#dateAdd('d', -90, now())#" cfsqltype="cf_sql_date">
      AND       purchasedate < <cfqueryparam value="#dateAdd('d', 1, now())#" cfsqltype="cf_sql_date">


If that's not what you mean, what are the desired results in plain english?
you want the returned recordset to be within 90 days of today ?

     WHERE  " . getdate(now()) . " BETWEEN purchasedate-90 AND purchasedate

i'm not sure of the exact syntax, but the logic should work.
if not, then . . .

     WHERE purchasedate BETWEEN " . getdate(now()) . " AND " . getdate(now()+90)
>> WHERE purchasedate BETWEEN " . getdate(now()) . " AND " . getdate(now()+90) <<

Is that valid sql server syntax?
Avatar of Tpaul_10

ASKER

I am sorry for not to be clear.
If I run the query today, I shouldn't get the dates that are older than 90 days, but should get any future dates
=============
PurchaseDate
=============
2011-12-12
2011-01-01
2010-07-05
2011-07-03
2011-05-01
2013-01-01
2012-01-01

So, my outout from the above data should be
=============
PurchaseDate
=============
2011-12-12
2011-07-03
2011-05-01
2013-01-01
2012-01-01

Thanks and hope that helps. I am going to try the Agx and DarkMain suggestions

ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial