首页 > 解决方案 > Comparing between two dates in Oracle DB is not giving proper result

问题描述

I've got two DATE columns in my table having below values:

Order_date  Service_date
01-06-17    31-01-18
01-06-17    01-10-18
01-06-17    01-07-18
01-06-17    01-04-18`

When I run the below query:

SELECT
    TRUNC(Order_date) Order_date,
    TRUNC(Service_date) Service_date,
    CASE
        WHEN trunc(Order_date) > trunc(Service_date) THEN 'Y'
        ELSE 'N'
    END actual
FROM
    table_x;

I get the result as:

Order_date  Service_date  actual
01-06-17    31-01-18      Y

The expected answer should be 'N'.

But if I run a similar query, by actually passing the DATE values, it gives me proper result.

SELECT
    to_date('01-06-17','DD-MM-RR') Order_date,
    to_date('31-01-18', 'DD-MM-RR') Service_date,
    CASE
        WHEN to_date('01-06-17','DD-MM-RR') > to_date('31-01-18', 'DD-MM-RR') THEN 'Y'
        ELSE 'N'
    END actual
FROM
    table_x;

Order_date  Service_date  actual
01-06-17    31-01-18      N

Why isn't it giving the the expected result with the original query?

标签: sqloracledatetime

解决方案


推荐阅读