在 SQL/ORACLE 中将列的数据类型“varchar”更改为“DATE”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21467607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the data type "varchar'' of a column to "DATE" in SQL/ORACLE
提问by Atul Kumar Verma
I have a table in a database created in oracle 10G. It contains a column of type 'VARCHAR' and stores date as string in this format-> 'dd-mon-yyyy' eg: '12-aug-2008'. Now I want to change the datatype of this column from VARCHAR to DATE. but when i perfrom this query->
我在 oracle 10G 中创建的数据库中有一个表。它包含一个类型为“VARCHAR”的列,并以这种格式将日期存储为字符串->“dd-mon-yyyy”,例如:“12-aug-2008”。现在我想将此列的数据类型从 VARCHAR 更改为DATE。但是当我执行此查询时->
ALTER TABLE sales_order
MODIFY COLUMN delivery_date DATE;
I get following error
我收到以下错误
ORA-00905: missing keyword
ORA-00905: 缺少关键字
I have also tried :
我也试过:
ALTER TABLE sales_order
ALTER COLUMN delivery_date DATE;
I got the error :
我收到错误:
ORA-01735: invalid ALTER TABLE option
ORA-01735: 无效的 ALTER TABLE 选项
However when i try to add a fresh column with DATE datatype it works fine. example :
但是,当我尝试使用 DATE 数据类型添加新列时,它工作正常。例子 :
ALTER TABLE sales_order
ADD delivery DATE;
So, can anybody suggest me a way to change the datatype without deleting the column and its data.
那么,任何人都可以建议我一种在不删除列及其数据的情况下更改数据类型的方法。
回答by paxdiablo
It's the first one, with a slight modification:
这是第一个,稍作修改:
ALTER TABLE sales_order MODIFY (delivery_date DATE);
But I'm not surethat will work for those particular datatypes and it also may not work depending on the current data.
但我不确定这是否适用于那些特定的数据类型,而且根据当前数据,它也可能不起作用。
You may find it necessary in that case to:
在这种情况下,您可能会发现有必要:
- create a new column
X
of date type. - populate
X
based on the old column (may need several passes of data fix-ups to work). - delete old column.
- rename
X
to old column name.
- 创建一个新
X
的日期类型列。 X
基于旧列填充(可能需要多次数据修复才能工作)。- 删除旧列。
- 重命名
X
为旧列名。
回答by Vimal
modify a column then syntax is:-
修改一列然后语法是:-
alter table table_name modify column_name datatype;
alter table table_name 修改 column_name 数据类型;
but when you modify the column datatype column must be empty
但是当您修改列数据类型时,列必须为空
回答by Megui
Thanks for the hints! This got it for me.
感谢您的提示!这是给我的。
alter table Table_Name Alter column Column_Name datatype GO
更改表 Table_Name 更改列 Column_Name 数据类型 GO
I too was needing to change from a VARCHAR to a date. I am working in SQL 2008 R2. I have found that if I bring in dates as a char or varchar and then change the type to date or datetime, I can catch time/date problems more easily.
我也需要从 VARCHAR 更改为日期。我在 SQL 2008 R2 中工作。我发现如果我将日期作为 char 或 varchar 引入,然后将类型更改为 date 或 datetime,我可以更轻松地捕获时间/日期问题。
THIS STATEMENT WILL FAIL IF YOUR DATA HAS ANY BAD DATES. (I break the date down into sections to find the bad date so I can correct and then I can alter the column type.)
如果您的数据有任何错误日期,则此声明将失败。(我将日期分解为多个部分以查找错误日期,以便我可以更正,然后我可以更改列类型。)
回答by Siddhartha Chowdhury
Although its a pretty old question, I'll put my solution here for people seeking for a solution:
虽然这是一个很老的问题,但我会将我的解决方案放在这里供寻求解决方案的人们使用:
Here's my solution and it works perfectly.
这是我的解决方案,它完美地工作。
ALTER TABLE `sales_order` CHANGE `delivery_date` `delivery_date` DATE;
Thank you
谢谢
回答by Clayne Okallo
Alternatively, you could create a new column, in which the data type is DATE. then pass the data in your varchar as a date .then drop your initial column and finally rename your new column to what it was initially...code below.
或者,您可以创建一个新列,其中数据类型为 DATE。然后将您的 varchar 中的数据作为日期传递。然后删除您的初始列,最后将您的新列重命名为最初的...代码如下。
ALTER TABLE my_table ADD (new_col DATE);
UPDATE my_table SET new_col=TO_DATE(old_col,'MM/DD/YYYY');
ALTER TABLE my_table DROP (old_col);
ALTER TABLE my_table RENAME new_col TO old_col;