Assuming LAST_TRANSACTION_DATE is a DATE (or TIMESTAMP ) column, both versions are very bad.
In both cases, the DATE column will be implicitly converted to a character literal based on the current NLS settings. This means that with different customers you will get different results.
When using date literals, always use to_date() with a (!) Format mask or use an ANSI date literal. This way you compare dates with dates, not strings with strings. Therefore, for equal comparison, you should use:
LAST_TRANSACTION_DATE = to_date('30-JUL-07', 'dd-mon-yy')
Note that using “MON” may still lead to errors with different NLS settings ( 'DEC' versus 'DEZ' or 'MAR' versus 'MRZ' ). Much less prone to errors using month numbers (and four-digit years):
LAST_TRANSACTION_DATE = to_date('30-07-2007', 'dd-mm-yyyy')
or using an ANSI date literal
LAST_TRANSACTION_DATE = DATE '2007-07-30'
Now the reason why the above query most likely returns nothing is because the Oracle DATE columns also indicate the time. The above date literals implicitly contain the time 00:00 . If the time in the table is different (for example, 19:54 ), then, of course, the dates are not equal.
To get around this problem, you have different options:
- use
trunc() in the column of the table to "normalize" the time until 00:00 trunc(LAST_TRANSACTION_DATE) = DATE '2007-07-30 this, however, will prevent the use of the index defined on LAST_TRANSACTION_DATE - use
between
LAST_TRANSACTION_DATE between to_date('2007-07-30 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and to_date('2007-07-30 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
The performance problem of the first solution can be solved by creating an index on trunc(LAST_TRANSACTION_DATE) , which can be used by this expression. But the expression LAST_TRANSACTION_DATE = '30-JUL-07' prevents the use of the index, because internally it is treated as to_char(LAST_TRANSACTION_DATE) = '30-JUL-07'
Important to remember:
- Never, never rely on implicit data type conversion. At some point this will cause problems. Always compare the correct data types.
- Oracle
DATE columns always contain time, which is part of the comparison rules.
a_horse_with_no_name
source share