473,383 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Date/time comparison problem

Jon
Hi,

I am trying to pick up records from a date range without much luck. For my
example below I am trying to pick up all records made in the last month.
However none are being found (my code is tried and tested for all records).
My database is in Access format. Field "datetime1" is of Date/Time data type
getting the default value by using now()

The relevant part of my code is as follows. Can anyone see what I'm doing
wrong?
Many thanks.
Jon.
----------------

Dim dateVar1 'From date
Dim dateVar2 'To date

dateVar1 = dateadd("m",-1,Now()) ' this time last month
dateVar2 = Now() 'now

strSQL = "SELECT * FROM Log WHERE datetime1 >= #" & dateVar1 & "# AND
datetime1 < #" & dateVar2 & "#"

Jul 19 '05 #1
2 2247
Can't you do this in the query? Something like:

sql = "SELECT <always,list,column,names!> FROM Log WHERE datetime1 >=
DATEADD(""m"", -1, NOW()) AND datetime1 < NOW()"

If you really, really, really want to pass the values in from ASP for some
reason, then make sure the dates are in YYYY-MM-DD format, so that
05/09/2004 is not mistaken for 09/05/2004, for example.

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/


"Jon" <Jo*@nospam.com> wrote in message
news:d3***************@newsfe1-gui.server.ntli.net...
Hi,

I am trying to pick up records from a date range without much luck. For
my
example below I am trying to pick up all records made in the last month.
However none are being found (my code is tried and tested for all
records).
My database is in Access format. Field "datetime1" is of Date/Time data
type
getting the default value by using now()

The relevant part of my code is as follows. Can anyone see what I'm doing
wrong?
Many thanks.
Jon.
----------------

Dim dateVar1 'From date
Dim dateVar2 'To date

dateVar1 = dateadd("m",-1,Now()) ' this time last month
dateVar2 = Now() 'now

strSQL = "SELECT * FROM Log WHERE datetime1 >= #" & dateVar1 & "# AND
datetime1 < #" & dateVar2 & "#"

Jul 19 '05 #2
Jon wrote:
Hi,

I am trying to pick up records from a date range without much luck.
For my example below I am trying to pick up all records made in the
last month. However none are being found (my code is tried and tested
for all records). My database is in Access format. Field "datetime1"
is of Date/Time data type getting the default value by using now()

The relevant part of my code is as follows. Can anyone see what I'm
doing wrong?
Many thanks.
Jon.
----------------

Dim dateVar1 'From date
Dim dateVar2 'To date

dateVar1 = dateadd("m",-1,Now()) ' this time last month
dateVar2 = Now() 'now

strSQL = "SELECT * FROM Log WHERE datetime1 >= #" & dateVar1 & "# AND
datetime1 < #" & dateVar2 & "#"


Jet queries can use some VBA functions, including Now(), Date() and DateAdd.
You do not need the "<" part of this statement. Change your strSQL
definition to:

strSQL = "... datetime1 >= DateAdd('m',-1,Date())"

I also suggest you eschew the use of selstar (Select *) in production code.
You are impairing performance because ADO has to make an extra trip to the
database to resolve the * into actual column names when you use selstar.

The first thing you should do when you have a problem with a query built via
dynamic sql is to use "Response.Write strSQL" to see the actual query being
sent to the database. If you have built the statement correctly, you should
be able to copy it from the browser window into the SQL View of the Access
Query Builder and run it without modification (the exception is if your
query uses LIKE with wildcards - you must use the ODBC wildcards (%, _)
instead of the Jet wildcards (*, ?) when using ADO to execute a dynamic sql
statement.

You are much better off using saved queries instead of dynamic sql. Search
for posts by me that contain "saved parameter query" for more details.

HTH,
Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel Fisher | last post by:
Hi All! I'm fairly new to PhP and basicly trying to learn right now. Now I have a problem - I have a fairly large collection of movies which people keep borrowing from me. And then not returning....
2
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
1
by: colinhumber | last post by:
I have a datetime variable coming from my ASP.NET application that has a time portion. I give my users the option to perform an equals, greater than, less than, or between comparison. The trouble...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
6
by: MarkAurit | last post by:
Im having difficulty coming up with a good algorithm to express the following comparison: "if <a given date> falls between the (current date - 5 days) and the (current date)" Obviously....
4
by: blini | last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need to convert and to compare if "09/06/2006 14:20" is lesser or equal that the current date. Everything in Javascript.
7
by: mr.nimz | last post by:
hello, this is antenio. recently i've come to a problem. i got a way through it, somehow, still it left me in a curious state, so i'm posting it here, if i can get an answer from some techy, ...
3
by: noone | last post by:
Hi, I am designing an application which displays news topics until midnight on the DisplayUntil date and then they should drop out. Unfortunately, they seem to be dropping out at mid-day. I'm...
4
by: anagai | last post by:
I just want to check if a date entered in a textbox is equal to the current system date. I set the date object from the input field like this: dt1=new Date('10/01/2007'); the current system...
16
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.