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

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;

Related

Convert varchar column to date

I have one column LogDate which is varchar(2000) type. I want this column as date type. Current table format 2014-03-31 09:13:03.000.
How can I do that?
I presume you are using SQL Server:
declare #LogDate varchar(2000)
set #LogDate = '2014-03-31 09:13:03.000'
select convert (datetime, #LogDate, 21)
Full table query at: http://sqlfiddle.com/#!3/58a3f/1/0
If all the records in the table having data in correct format(i.e all are date only, no characters) then you can directly alter the column data type to DATE type:
ALTER TABLE <table name> ALTER COLUMN LogDate date

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.

Alter character field to date

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.