Convert varchar to date in sql loader - sql

I have loaded the date field with dates and type is varchar.
How to convert date field(varchar) to date field(date) in oracle express/sql loader while displaying the fields?

You can't change the data type of a column in a permanent table from VARCHAR2 to DATE when it has data.
You can, however, add a new column
ALTER TABLE table_name
ADD( new_date_column DATE );
move the data over
UPDATE table_name
SET new_date_column = to_date( old_varchar2_column, format_mask );
drop the old column
ALTER TABLE table_name
DROP COLUMN old_varchar2_column;
and then rename the new column to the old column name
ALTER TABLE table_name
RENAME COLUMN new_date_column TO old_column_name
Of course, once you do this, you'll need to change your SQL*Loader script to convert the data to a DATE if you ever want to load into this table again.

Related

Converting date column to varchar column in Oracle with existing data

I need to change column type from DATE to VARCHAR2. Currently, date column has so many records which are inserted previously and now I need to change column type from DATE to VARCHAR2 without losing any existing data. Please suggest me how can I do it?
simple demostration:
create table t (c date);
insert into t values (sysdate);
alter table t add c_tmp varchar2(20);
update t set c_tmp = c;
alter table t drop column c;
alter table t rename column c_tmp to c;
drop table t;
however, I don't recommend for most of the circumstances to convert data type from date to varchar2. It does not provide clear benefit on my point of view.

PostgreSQL alter column type from DATERANGE to TSTZRANGE and migrate the data

We're changing a column run_dates (per request) from daterange to tstzrange, a column with a few months of test data. Essentially we want to migrate!
I'm running the script below, inspired by postgreSQL alter column data type to timestamp without time zone and I have to admit, my SQL background is basically nothing
-- Create a temporary TIMESTAMP column
ALTER TABLE table_name ADD COLUMN run_dates_holder TSTZRANGE NULL;
-- Copy casted value over to the temporary column
UPDATE table_name SET run_dates_holder = run_dates::TSTZRANGE;
-- Modify original column using the temporary column
ALTER TABLE table_name ALTER COLUMN run_dates TYPE
TSTZRANGE USING run_dates_holder;
-- Drop the temporary column (after examining altered column values)
ALTER TABLE table_name DROP COLUMN run_dates_holder;
Unfortunately..... These types don't naturally translate.
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) cannot cast type daterange to tstzrange
and
psycopg2.ProgrammingError: cannot cast type daterange to tstzrange
LINE 5: ...ble_name SET run_dates_holder = run_dates::TSTZRANG...
Has anyone ever successfully migrated a daterange to a tstzrange?
As a backup, we can def just drop the daterange column and recreate as tstzrange, since that would only affect test data. Just less optimal for the team. It's worth a shot to look into this, and I think the resolution here is at least worthwhile to future 'daterange-to-tstzrange' migrators, as I found no other docs/resources on the matter
You can't just cast a daterange to a tstzrange. Use lower() and upper() to extract the bounds of the daterange and upper_inc() and lower_inc() to extract their inclusivity, and construct a new tstzrange.
UPDATE table_name
SET run_dates_holder=tstzrange(
lower(run_dates), upper(run_dates),
concat(
CASE WHEN lower_inc(run_dates) THEN '[' else '(' END,
CASE WHEN upper_inc(run_dates) THEN ']' ELSE ')' END)
);
Using the line from AdamKG's answer (selected as the BEST!) we arrive at this
-- -- Create a placeholder tstzrange column
ALTER TABLE SB ADD COLUMN run_dates_holder tstzrange NULL;
-- -- Update placeholder tstzrange column with existing run_dates
UPDATE SB SET run_dates_holder=tstzrange(lower(run_dates), upper(run_dates),'[)');
-- -- Remove original run_dates
ALTER TABLE SB DROP COLUMN run_dates;
-- -- Rename 'run_dates_holder' to 'run_dates'
ALTER TABLE SB RENAME COLUMN run_dates_holder to run_dates;
Test data's remained intact

change all timestamp columns in table to varbinary(8)

I'm creating an archive table using SELECT INTO. This table however has a time stamp column, and when I try to insert values I get the following error:
Cannot insert an explicit value into a timestamp column
I do not wish to insert using a column list, and then insert null to the TimeStamp column
What I want to do is on the archive table change dynamically all colums of type Timestamp to varbinary(8).
What I have tried so far is to get the column:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'AppServices_Notification' AND
DATA_TYPE = 'TimeStamp'
ALTER TABLE AppServices_Notification
ALTER COLUMN COLUMN_NAME varbinary(8)
Is there a way to this in one action?

T-SQL computed date column

I have a table, in TSQL, with a field containing data in YYYYMMDD format saved as varchar(50);
I want to add a date type column to the table for each of the corresponding records in this field.
Any ideas?
Assuming that you have stored correct format of date in your field (eg you don't have '20121433'), this script should works for you:
ALTER TABLE your_table
ADD your_field_Date DATETIME
UPDATE your_table
SET your_field_Date = CONVERT(DATETIME, your_field_varchar, 112)
ALTER TABLE your_table DROP COLUMN your_field_varchar

Converting varchar column to date column in Oracle

I need to change column type from VARCHAR2 to DATE. The column is already storing dates in correct format. So I was trying to do something like this
alter INM_INCIDENT rename column INM_I_REACTION_TIME to INM_I_REACTION_TIME_OLD;
alter table INM_INCIDENT add INM_I_REACTION_TIME date;
update INM_INCIDENT set INM_I_REACTION_TIME = to_date(INM_I_REACTION_TIME_OLD);
alter table INM_INCIDENT drop column INM_I_REACTION_TIME_OLD;
But I've got error on the line with update statement, so my question is, is there any nice solution for copying varchar to date like this?
Update:
update INM_INCIDENT set INM_I_REACTION_TIME = to_date(INM_I_REACTION_TIME_OLD, 'YYYY-MM-DD HH24:MI:SS');
You have to adjust the string YYYY-MM-DD HH24:MI:SS to your date format in your string date column.