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.
Related
The date format in the table is YYYYMMDD and I would like to convert it to the following format but it is failing with an error:
2019-07-23 00:00:00.000
Conversion failed when converting date and/or time from character string
Here is the statement I'm using:
convert(varchar(10), convert(datetime, InstallDate0), 23)
The real problem is the choice of your datatype. varchar is the wrong choice. As a result, it seems that you now have some rows where the value of the "date" has been lost, as it can't be converted to a date.
To properly fix this problem, fix your datatype. Firstly I would create a new column to store the bad values:
ALTER TABLE YourTable ADD BadDate varchar(20); --as it's yyyyMMdd you don't need more than 8 characters, but we'll assume you have some really bad values
UPDATE YourTable
SET BadDate = InstallDate0
WHERE TRY_CONVERT(datetime,InstallDate0) IS NULL;
Now that you've done that, time to update the existing column:
UPDATE YourTable
SET InstallDate0 = CONVERT(varchar(8),TRY_CONVERT(datetime, InstallDate),112);
This'll set every value to the yyyyMMdd format where the value can be converted. NOw you can alter your table:
ALTER TABLE YourTable ALTER COLUMN InstallDate0 date; --AS it's yyyyMMdd, it seems silly to actually use datetime
Now you have a proper datetime column.
You'll then need to inspect the values of BadDate and try to correct them (or admit that any information they held has been lost for ever).
If you "must" have another column with the format, then add a further column:
ALTER TABLE YourTable ADD InstallDate0_f AS CONVERT(varchar(23),InstallDate0,121);
You can determine where the problems are using TRY_CONVERT(). The problem would seem to be the conversion to a datetime, so try this:
select InstallDate0
from t
where try_convert(datetime, InstallDate0) is null;
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
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.
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.
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;