Solved

comparing a datetime in WHERE clause inMFC CRecordset

Posted on 2000-03-30
6
419 Views
Last Modified: 2010-08-05
I'm using MFC CRecordset class to select records from an ODBC Data Source (Access).  I'm trying to set a filter for a datetime column (named "Date") compared to a value I'm building from data in my program.

for values of m_strFilter, I've tried tried:
"Date > '03/30/00 10:00:00'"
"Date > '10:00:00 03/30/00'"

... and just about every variation on the date format.  No records are selected (triple checked that they should have been).  I also tried leaving out the time, and comparing against the date only.

I saw some articles referencing the CONVERT function, so I tried that ("Date > CONVERT(datetime,'03/30/00')") However, that gives a CDatabaseException with reason: "CONVERT is an unknown function"
0
Comment
Question by:sniles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2672051
Try this:
"[Date] > #2000/03/30 10:00:00#"
or
"[Date] > #2000/03/30#"
0
 

Author Comment

by:sniles
ID: 2673392
I tried those, but they did not work.  With no single quotes around the date field (as in your suggestion), nothing was selected.  When no single quotes were used a record from 3/28/00 and the record from 3/30/00 was selected.
0
 
LVL 10

Expert Comment

by:paasky
ID: 2673587
Hello sniles,

Use CDate() with Access database:

Date > CDate('03/30/00 10:00:00')
Date > CDate('10:00:00 03/30/00')

It converts value to valid date.

Hope this helps,
Paasky
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:sniles
ID: 2676348
Thanks!  That did work.  Is this is a general solution?  You specifically mentioned Access -- will this also work with SQL Server (& other ODBC)?
0
 
LVL 10

Accepted Solution

by:
paasky earned 200 total points
ID: 2677217
Happy to help you sniles,

Unfortunately there's no general solution that will work for all ODBC sources. You need to use the specific conversion function of the specific database you're using.

CDate('03/30/00 10:00:00') works with Access.

CONVERT(DateTime, '03/30/00 10:00:00') works with SQL Server

TO_DATE('03/30/00 10:00:00','MM/DD/YY HH24:MI:SS') works with Oracle.

Regards,
Paasky
0
 

Author Comment

by:sniles
ID: 2677337
Thanks again!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question