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

Problem querying using Now in a database table.

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
I execute a simple DELETE on a table to remove old records earlier than 15 minutes ago
The date column (Scheduled) is formatted as a Date/Time and contains a date and time for each row.

dbS.Execute "DELETE Scheduled From MyTable WHERE Scheduled < DateAdd('n',-15,Now());"

This works fine for times from 00:01 am to 11:59 pm but does not remove any records when executed just past midnight if there are any entries still there from the previous day such as -
Say there are 5 records either side of midnight.
3/4/2015 11:55 pm
3/4/2015 11:58 pm
4/4/2015 0:03 am
4/4/2015 0:05 am
4/4/2015 0:10 am

If I run the query 4/4/2015 0:19 am it should remove only the first three entries - but it doesn't remove any!
It works fine around midday (am's & pm's reversed).

What am I doing wrong in my execute statement? Do I have to somehow separately consider Date and Time rather than use Now?

This is used in a bus departure sign running 24/7 but luckily it does not affect the end result because the bus has gone by then and removed from the screen anyway but it worries me that I can't find the reason.
 
I worked out another way of doing it that works using IIF.

Code:
dbS.Execute "DELETE IIf([Scheduled] > DateAdd('n',-15,Now()),0,1) AS Expr " _
& "From MyTable " _
& "WHERE ((IIf([Scheduled] > DateAdd('n',-15,Now()),0,1))=1);"

This removes all Schedule entries more than 15 minutes ago

I can't see why the earlier one doesn't!
 
That was my exactly first example that doesn't always work. As I said it does not work when the times span either side of midnight.
Maybe it is because I use International date format dd/mm/yyyy but how do I put #'s around Now!

The second example has to be the other way around because it is creating a 0 if before [Scheduled] or 1 if later. The where-ing is then on the 1 instead of the date. Thats why it works.

I probably only needed

Code:
dbS.Execute "DELETE From MyTable WHERE ((IIf([Scheduled] > DateAdd('n',-15,Now()),0,1))=1);"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top