I have a table say demo with three columns
create table demo(demo_id number,
created_date date,
mod_date systimestamp(6)
);
my requirement is to return a refcursor with two columns
1. Demo_id
2. Status column
Which will be derived as follows:
if created_date = mod_date then "New" else "Updated" .
So I have written my code as :
select demo_id ,case when created_date=mod_date then "New" else "update" end from demo;
but somehow even though the dates are same including the timecomponent My status value is always showing updated. I am using SQL developer to run my queries and have also modified the nls_date_format to be DD/mm/yyyy hh24:mi:SS. Also mod_date will be storing systimestamp.
You are comparing a date, which has a second precision, with a timestamp, which has fractional-second precision.
If
created_dateis taken from sysdate andmod_dateis taken from systimestamp then you could be comparing something like2022-08-06 23:36:58with2022-08-06 23:36:58.373657. Those are not the same.The data type comparison and precedence rules mean that the date value is implicitly converted to a timestamp, but that just means it's now comparing
2022-08-06 23:36:58.000000with2022-08-06 23:36:58.373657. Those are still not the same.As you can't add precision to the date, you will have to remove precision from the timestamp, which you can do by casting it to that data type:
db<>fiddle
However, if the row is modified during the same second it was created - e.g. from the example used before, if
mod_dateis set to2022-08-06 23:36:58.999999- then that won't be seen as an update, since once the precision is reduced the original and updatedmod_dateare identical, and both the same ascreated_date.That might not be an issue for you, but it would still be simpler if you made
created_datea timestamp as well, and set that withsystimestamptoo.