I have a DB2 DATE type field in a DB2 table. I want to select data by a date filter. For example:
SELECT *
FROM table
WHERE registrationdate > '2002-10-01';
From the above query, I get records with registrationdate starting from '1943-10-01', but this is incorrect.
These do not work either:
registrationdate > date('2002-10-01')
date(registrationdate) > date('2002-10-01')
date(registrationdate) > '2002-10-01'
How do I need to compare dates?
Answer. The Date/Time wizard (found under Transform -> Date/Time) can calculate the difference between two dates with results in days, months or years.
This can be easily done using equals to(=), less than(<), and greater than(>) operators. In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement.
A date, time, or timestamp value can be compared with another value of the same data type, a datetime constant of the same data type, or with a string representation of a value of that data type. Additionally, a TIMESTAMP WITHOUT TIME ZONE value can be compared with a TIMESTAMP WITH TIME ZONE value.
Answer. The SQL99 (SQL3) Standard states that the DATE, TIME, and TIMESTAMP data types are not compatible for either assignment or comparison. DB2 for Linux, UNIX, and Windows and WebSphere Federation Server enforce this restriction.
The SQL standard format for a DATE literal is:
DATE '2002-10-01'
At the very least, it is worth trying:
SELECT *
FROM table
WHERE registrationdate > DATE '2002-10-01';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With