Change date column to timestamp

Asked

Viewed 1,280 times

0

I have a table with a column like date but I need to see the GMT information, my idea is to convert it to timestamp. How can I change that even if the column already contains value?

create table PRO_TFESTIVO
(
  oid_festivo      NUMBER(10) not null,
  fecha_hora_envio DATE to TIMESTAMP
)

1 answer

1

You cannot change the type of a DATE/TIMESTAMP column without:

  1. Rename column (Ex: OLD_FECHA_HORA_ENVIO) to existing values;
  2. Create a new column with the original name (FECHA_HORA_ENVIO);
  3. Fill this new column with the existing values in the renamed table;
  4. Remove the column (OLD_FECHA_HORA_ENVIO) as we will no longer use it;

Convert from DATE to TIMESTAMP WITH TIME ZONE:

ALTER TABLE pro_tfestivo RENAME COLUMN FECHA_HORA_ENVIO TO OLD_FECHA_HORA_ENVIO;
ALTER TABLE pro_tfestivo ADD FECHA_HORA_ENVIO TIMESTAMP WITH TIME ZONE;
UPDATE pro_tfestivo SET FECHA_HORA_ENVIO = FROM_TZ(CAST(OLD_FECHA_HORA_ENVIO AS TIMESTAMP), 'GMT');
ALTER TABLE pro_tfestivo DROP COLUMN OLD_FECHA_HORA_ENVIO;

Extra :) Convert from TIMESTAMP WITH TIME ZONE to DATE:

ALTER TABLE pro_tfestivo RENAME COLUMN FECHA_HORA_ENVIO TO OLD_FECHA_HORA_ENVIO;
ALTER TABLE pro_tfestivo ADD FECHA_HORA_ENVIO DATE;
UPDATE pro_tfestivo SET FECHA_HORA_ENVIO = CAST(to_timestamp_tz(OLD_FECHA_HORA_ENVIO, 'dd/mm/yyyy hh24:mi:ssXFF TZR') at time zone 'GMT' AS DATE);
ALTER TABLE pro_tfestivo DROP COLUMN OLD_FECHA_HORA_ENVIO;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.