Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update Oracle Timestamp value from the current to a timestamp from the past

Tags:

sql

oracle

I have an Oracle Table called EVENT_TABLE_T. It has a column called LAST_UPDATE_DT. One sample value from this column is: 01-JUL-13 11.20.22.37448900 AM. There are over 700 rows that have this same timestamp value. I would like to update this value to 45 days before this date, using a SQL statement. For example,01-JUL-13 11.20.22.37448900 AM, after my mental arithmetic, should become: 15-May-13 11.00.00...... (exactly 45 days). If this is successful, I would like to apply an update on a different value in LAST_UPDATE_DT that reflects a value that goes back back 46 days.

What I hope to accomplish by asking this question is to be able to learn the basics of Oracle dates and timestamps and apply them to my batch processing work. I would like to be able to run this update sql statement from Oracle SQL Developer and also from inside a Java PreparedStatement.

Thanks in advance for your help.

like image 662
ilango Avatar asked Jul 01 '13 18:07

ilango


People also ask

How do I change the TIMESTAMP in SQL?

select to_char(sysdate, 'YYYY-MM-DD') from dual; To get this format by default, set it in your session's NLS_DATE_FORMAT parameter: alter session set NLS_DATE_FORMAT = 'YYYY-MM-DD'; You can also set the NLS_TIMESTAMP_FORMAT and NLS_TIMESTAMP_TZ_FORMAT .

Can we convert date to TIMESTAMP in Oracle?

If you want to convert a DATE datatype to a TIMESTAMP datatype format, just use the CAST function. As you can see, there is a fractional seconds part of '. 000000' on the end of this conversion.

What is the difference between current TIMESTAMP and Systimestamp?

SYSTIMESTAMP returns current timestamp on database server, while current_timestamp returns current timestamp on client machine. So if your database server is in New York and client box is in California, SYSTIMESTAMP will be 3 hours ahead of CURRENT_TIMESTAMP.


1 Answers

You can simply subtract a time interval from the timestamp.

UPDATE EVENT_TABLE_T 
SET LAST_UPDATE_DT = last_update_dt - interval '45' day
WHERE LAST_UPDATE_DT = TO_TIMESTAMP('01-JUL-2013 11:20:22:37448900','DD-MON-YYYY HH24: MI:SS:FF')
like image 108
Dave Costa Avatar answered Nov 06 '22 04:11

Dave Costa