Solved

comparing a datetime in WHERE clause inMFC CRecordset

Posted on 2000-03-30
6
411 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
  • 3
  • 2
6 Comments
 
LVL 142

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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now