I have a TIMESTAMP(6)
column filled with data. Time information is not important anymore. How can I migrate this to the DATE
column and save data?
What is the easiest way to do this in SQL?
I have a TIMESTAMP(6)
column filled with data. Time information is not important anymore. How can I migrate this to the DATE
column and save data?
What is the easiest way to do this in SQL?
A DATE
column always has year, month, day, hour, minute and second components so converting to a DATE
is not going to eliminate those time components (but will discard the fractional seconds stored in the TIMESTAMP
).
Add the new column:
ALTER TABLE table_name ADD new_date_column DATE;
Then use an implicit cast:
UPDATE table_name
SET new_date_column = old_timestamp_column;
Or an explicit cast:
UPDATE table_name
SET new_date_column = CAST( old_timestamp_column AS DATE );
Then you can review the changes and drop the old column.
If you want to set all the time components to zero then you can use TRUNC
:
UPDATE table_name
SET new_date_column = TRUNC( old_timestamp_column );
If you just want to change the column's data type without reviewing the data then use VBokšić's answer or modify the column and then use TRUNC
to zero the time components:
ALTER TABLE table_name MODIFY column_name DATE;
UPDATE table_name SET column_name = TRUNC( column_name );
I believe you can do direct modify to this column and all will be ok:
ALTER TABLE testTable
MODIFY test_c date;