Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / SQL Server / ADP / August 2005

Tip: Looking for answers? Try searching our database.

Comparing dates in a query statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wes Peters - 24 Aug 2005 14:09 GMT
I want to be able to compare a user-supplied date against a smalldatetime
field in a query.  It looks like using the # sign is a mdb thing only.  Do I
use the Convert function on the table's date field to convert to a
"formatted" smalldatetime and then compare?  This is what I've tried with
some success.  It works when I use a > or < in the comparison but not =.

What is the best approach to constructing a query string that compares a
user-supplied date against smalldatetime field?

Any thoughts or articles on the subject would be appreciated.

Thanks,
Wes
Sylvain Lafontaine - 24 Aug 2005 16:52 GMT
On SQL-Server, dates must be enclosed with single quote ' instead of the
pound sign #.  With the correct option set, you can also use the double
quote " but it's better to stick with the standard.

You must use the international date format, yyyy/mm/dd or the US format,
mm/dd/yyyy or mm/dd/yy if the default language for the login is english;
otherwise you must use an appropriate format.

The trouble with DateTime and SmallDateTime is that they have both a time
part that may be different from midnight; something that may cause a lot of
problem when used with the equality operator = .  The convert function can
be used to strip away this time part; use a second call to the convert
function to convert back to a DateTime or SmallDateTime field if you don't
want some missy resultats with > and < .

Finally, the best way is not to construct the query string but to use a
query with parameters; as this will shield you from potential localization
problems (and security/hacking problems, too).

Signature

Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF

>I want to be able to compare a user-supplied date against a smalldatetime
> field in a query.  It looks like using the # sign is a mdb thing only.  Do
[quoted text clipped - 10 lines]
> Thanks,
> Wes
Wes Peters - 24 Aug 2005 22:04 GMT
Thanks for the info.

Does the following statement strip the time from a datetime field:

CONVERT (datetime, CONVERT (varchar, tblOrders.DTStamp, 101), 101) AS Placed

When I compare the above field with the same date in an unbound textbox
using single quotes they don't show to be equal and I'm not sure why unless
somehow the time part is still there.

Any thoughts?

> On SQL-Server, dates must be enclosed with single quote ' instead of the
> pound sign #.  With the correct option set, you can also use the double
[quoted text clipped - 29 lines]
> > Thanks,
> > Wes
Sylvain Lafontaine - 24 Aug 2005 22:38 GMT
Yes, this should be sufficient to strip the time (or more exactly reset it
to midnight).  Personally I use nvarchar(10) instead of varchar but that
shouldn't change anything.

I don't know why this doesn't work with your unbound textbox.  You should
print the intermediary result to make sure that everything is OK.  Apply the
Convert() function to the value of your textbox to see how it is interpreted
by SQL-Server.

Signature

Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF

> Thanks for the info.
>
[quoted text clipped - 52 lines]
>> > Thanks,
>> > Wes
Wes Peters - 25 Aug 2005 14:35 GMT
Thanks for your help Sylvain.

I finally got it working.  Long story short, I was comparing the text box
date against a date field I *thought* I had CONVERTed but hadn't.

Wes

> Yes, this should be sufficient to strip the time (or more exactly reset it
> to midnight).  Personally I use nvarchar(10) instead of varchar but that
[quoted text clipped - 61 lines]
> >> > Thanks,
> >> > Wes
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.