Sql – Oracle Timestamp Conversion with Dates

dateoraclesqltimestamp

Assuming this has a simple solution, but I can't find it.

I'm trying to do some logic on a DATE field in Oracle. My desire is to take a DATE field and subtract X hours from it.

For instance: SELECT A.MyDATE - 100 Hours from dual;

however, I need a result in a timestamp format 'YYYY-MM-DD hh:mm'.

I've tried CAST(A.MyDATE as TIMESTAMP) - NUMTODSINTERVAL(100/24,'day') however it didn't work.

I found out that the issue is that the MyDATE field when cast to a timestamp still contained some residual time elements. How can I reset these??

Thanks!

Best Answer

You can just do this with subtraction:

select a.MyDate - 100.0/24

To convert to varchar:

select to_char(a.MyDate - 100.0/24, 'YYYY-MM-DD')

And, if you want to get rid of that pesky time on the date:

select trunc(a.MyDate - 100.0/24) as JustTheDate