Trouble with sql query using Access Database
im using Access as the database and im having a problem with the query below. I know the problem is with the email_date field but I dont know how to fix it. The problem is a text field which im trying to make use it as a datefield by using CDATE and compare it to a actual datefield which is causing the problem. Im getting the data mismatch error. In the email_date field if there is nothing, the field consists of '--', else it has 9/21/2011. Any help would be very much appreciated.
SELECT A.ICAO, A.IATA, A.AIRPORT_NAME, A.CITY, A.COUNTRY,
A.REVISED_DATE, A.COMPANY, A.EMAIL_DATE
FROM AIRPORT_CHECKLIST A
WHERE A.COMPANY = 'company'
AND FLAG_DELETE = 'No' 开发者_JAVA技巧
AND EMAIL_DATE <> '--'
AND CDATE(REVISED_DATE) > CDATE(EMAIL_DATE)
You can add a nested IIf() expression in the WHERE clause: return False if EMAIL_DATE is "--"; otherwise return True or False depending up the CDate() comparison of the two fields. A row will only be included in the result set if that expression returns True.
SELECT
A.ICAO,
A.IATA,
A.AIRPORT_NAME,
A.CITY,
A.REVISED_DATE,
A.COMPANY,
A.EMAIL_DATE
FROM
AIRPORT_CHECKLIST AS A
WHERE
A.COMPANY='company'
AND IIf(EMAIL_DATE='--',False,
IIf(CDATE(REVISED_DATE) > CDATE(EMAIL_DATE),True,False))
AND A.FLAG_DELETE='No';
Also if both "date" fields were text type, you could store the date values in "yyyy-mm-dd" format and simply compare the text values with no data type mismatch problems.
A.EMAIL_DATE <> '--' AND A.REVISED_DATE > A.EMAIL_DATE
Edit: If FLAG_DELETE is Yes/No type (instead of text type), use a literal False in the comparison.
AND A.FLAG_DELETE=False
The last line filters what ends up in the resultset, but does not prevent the last line from being run on those records. When EMAIL_DATE = '--' the last line causes your type mismatch.
One way to prevent this would be to use a subquery to filter out the empty dates first:
SELECT * FROM
(SELECT A.ICAO, A.IATA, A.AIRPORT_NAME, A.CITY, A.COUNTRY,
A.REVISED_DATE, A.COMPANY, A.EMAIL_DATE
FROM SELECTAIRPORT_CHECKLIST A
WHERE A.COMPANY = 'company'
AND FLAG_DELETE = 'No'
AND EMAIL_DATE <> '--')
WHERE CDATE(REVISED_DATE) > CDATE(EMAIL_DATE)
Change the line: AND FLAG_DELETE = 'No' to AND FLAG_DELETE = 0
精彩评论