Alter character field to date - sql

I've a legacy postgres db that has date columns cast as character(50) fields (don't ask). I'd like to alter the table and columns to contain actual dates. Because this worked:
select distinct to_date(date_begin, 'YYYY DD MM') from dates;
I naively thought this might work:
alter table dates alter column date_begin type character
using to_date(date_begin, 'YYYY DD MM');
But it does not. Any clues for the clueless?

This just works as intended by the OP. What we have here is a simple thinko/typo.
Read more in the manual about ALTER TABLE.
Demo:
-- DROP SCHEMA x CASCADE;
CREATE SCHEMA x;
CREATE TABLE x.tbl(date_begin character(50));
INSERT INTO x.tbl VALUES ('2011-11-11 11:11'), (NULL), (''), ('1977');
-- NULL and empty string work too
-- even just YYYY works: '1977' .. is converted to '1977-01-01' automatically
-- empty string produce a possibly surprising result: '0001-01-01 BC'
ALTER TABLE x.tbl ALTER COLUMN date_begin TYPE date USING to_date(date_begin, 'YYYY DD MM');
SELECT * FROM x.tbl;
Hint: You wrote type character instead of type date.

It will take three stages:
1) Alter the table to add the date column.
2) Run an update query which converts each string date into the date field
3) Alter the table to remove the text date column.

Related

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

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

Convert varchar to date in sql loader

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.

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.

How to convert column type from varchar to date in PostgreSQL?

I have varchar data type column and date data type column.
I have to update varchar column data into date column in PostgreSQL.
Is it possible?
Thanks.
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE
using to_date(<columnname>, 'YYYY-MM-DD');
UPDATE tableName SET dateColumn=to_date(varcharColumn, 'DD MM YYYY')
Assuming you are saving "07 04 2010"
You can find further examples and explanation in the documentation:
http://www.postgresql.org/docs/current/interactive/functions-formatting.html
to_date('05 Dec 2000', 'DD Mon YYYY')
syntax for typecasting:
alter table table_name alter column_name
type converting_data_type using(column_name::converting_data_type)
converting from varchar to date
alter table table_name
alter column_name type date using(column_name::date)
To convert column type from timestamp to date in postgresql with explicit typecast:
Explicit typecast allows our existing data to be converted to our new type when the column type is updated.
There is slight change in syntax for explicit typecast for keyword USING
Syntax:
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type
USING expression;
eg. Suppose we have table user_data with a column date_of_birth which took timestamp earlier, but now we want it to store only date.
Query:
ALTER TABLE user_data
ALTER COLUMN date_of_birth TYPE date
USING date_of_birth::date;