Solved

comparing a datetime in WHERE clause inMFC CRecordset

Posted on 2000-03-30
6
421 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

630 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