Subtracting a set number of days from a Date/Timestamp

Dear EE community,

Just for my peace of mind, would you please check that I'm doing this properly. Basically I'm trying to subtract a set number of days from a DateTime or Timestamp. This value then gets used in the WHERE on the SQL query.

In this example, excluding all customers that have been contacted within the last 90 days: now() - 90?

Please see below for what I am currently using in terms of code.

Thank you.
<!-- snippet of code -->
Dim rsQuery
Dim rsQuery_numRows
SQLQuery = "select QueryID, QueryName, ExcludeStartDate, ExcludeEndDate, IncludeStartDate, IncludeEndDate, ExcludeContactedDays, PartCodes from storedqueries where QueryID = '"&Session.Contents("scQID")&"'"
Set rsQuery = OBJdbConn2.Execute(SQLQuery)
rsQuery_numRows = 0
Dim mydate
Dim myday, myhour
Dim myminute, mysecond, myyear, mymonth
mydate = Now() '-- returns the current date and time
myday = Day(mydate) '-- returns the day (int; 1-31)
myhour = Hour(mydate) '-- returns the hour (int; 0-23)
myminute = Minute(mydate) '-- returns the minute(int; 0-59)
mysecond = Second(mydate) '-- returns the second (int; 0-59)
myyear = Year(mydate)'-- returns the year (int)
mymonth = Month(mydate)'-- returns the month (int;1-12)
Dim ExcStartDate
Dim ExcEndDate
Dim IncStartDate
Dim IncEndDate
Dim ExcContactedDays
Dim SPartCodes
QueryName = rsQuery("QueryName")
ExcStartDate = rsQuery("ExcludeStartDate")
ExcEndDate = rsQuery("ExcludeEndDate")
IncStartDate = rsQuery("IncludeStartDate")
IncEndDate = rsQuery("IncludeEndDate")
ExcContactedDays = myday-rsQuery("ExcludeContactedDays") '-- THIS IS THE ONE HERE HERE HERE!
SPartCodes = rsQuery("PartCodes")
<!-- more codey stuff and then the SQL bit -->
<!-- snippet of code again -->
If ExcContactedDays <> "" Then
pm.SetSource = pm.SetSource & "  `callnotes`.`CallBackStatus` NOT IN ('2', '3', '4', '5', '6', '7', '9', '10') and "
pm.SetSource = pm.SetSource & "  `callnotes`.`CreatedDate` >= '"&ExcContactedDays&"' and "
End If

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DateAdd("d", -90, Date())
just drop most of your VB code and use pure SQL :
pm.SetSource = pm.SetSource & "  `callnotes`.`CreatedDate` >= NOW() - INTERVAL 90 DAY and "

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BenthamLtdAuthor Commented:
Well I'll be damned. I've been doing SQL for a while now and didn't even know that command existed. Boy I feel stupid. Thank you "psadac" and thank you everyone for all the ideas. Especially the VBDate manipulation. That is also worth it's weight in gold.

Kindest regards.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.