在 SQL 中将整列更改为日期类型

发布于 2025-01-19 20:36:45 字数 399 浏览 4 评论 0原文

我正在尝试将列的数据类型cresence_date转换为日期类型,但无法执行此操作,该列是varchar type和ser_muhammad_mallick.user的列。 IAM使用以下代码段。

SELECT CONVERT (datetime, Confirmation_Date , 104) FROM USER_MUHAMMAD_MALLICK.user_upsells;

运行此代码时,我将收到以下错误消息

SQL Error [42000]: syntax error, unexpected IDENTIFIER_LIST_ [line 1, column 17] (Session: 1729360210797461536)

I am trying to convert the datatype of a column Confirmation_Dateto date type but unable to to do so, the Column is of varchar type and SER_MUHAMMAD_MALLICK.user is the tablel. Iam using the following code snippet.

SELECT CONVERT (datetime, Confirmation_Date , 104) FROM USER_MUHAMMAD_MALLICK.user_upsells;

I am receiving the following error message when I run this code

SQL Error [42000]: syntax error, unexpected IDENTIFIER_LIST_ [line 1, column 17] (Session: 1729360210797461536)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2025-01-26 20:36:45

您可以使用 Alter Table Modify column 。例如,将varchar列转换为date

create schema s;
create table t(c varchar(2000000));
insert into t values '2020-01-01', '2021-05-30';

/*
COLUMN_NAME SQL_TYPE              NULLABLE DISTRIBUTION_KEY PARTITION_KEY 
----------- --------------------- -------- ---------------- ------------- 
C           VARCHAR(2000000) UTF8 TRUE     FALSE            FALSE         
*/
describe t;
alter table t modify column c date ;
/*
COLUMN_NAME SQL_TYPE NULLABLE DISTRIBUTION_KEY PARTITION_KEY 
----------- -------- -------- ---------------- ------------- 
C           DATE     TRUE     FALSE            FALSE         
*/
describe t;

请注意,varchar值必须是正确的日期格式。如果您有不同的日期格式,则需要指定nls_date_format session参数, Alter Session

create or replace table t(c varchar(2000000));
insert into t values '01.01.2020', '30.05.2020';
-- data exception - invalid character value for cast; Value: '01.01.2020' Format: 'YYYY-MM-DD'
alter table t modify column c date ;

alter session set NLS_DATE_FORMAT='dd.mm.yyyy';

-- works now
alter table t modify column c date ;

You can use alter table modify column. For example to convert a VARCHAR column to DATE:

create schema s;
create table t(c varchar(2000000));
insert into t values '2020-01-01', '2021-05-30';

/*
COLUMN_NAME SQL_TYPE              NULLABLE DISTRIBUTION_KEY PARTITION_KEY 
----------- --------------------- -------- ---------------- ------------- 
C           VARCHAR(2000000) UTF8 TRUE     FALSE            FALSE         
*/
describe t;
alter table t modify column c date ;
/*
COLUMN_NAME SQL_TYPE NULLABLE DISTRIBUTION_KEY PARTITION_KEY 
----------- -------- -------- ---------------- ------------- 
C           DATE     TRUE     FALSE            FALSE         
*/
describe t;

Note that the varchar values must be of the correct date format. If you have a different date format you need to specify the NLS_DATE_FORMAT session parameter with ALTER SESSION:

create or replace table t(c varchar(2000000));
insert into t values '01.01.2020', '30.05.2020';
-- data exception - invalid character value for cast; Value: '01.01.2020' Format: 'YYYY-MM-DD'
alter table t modify column c date ;

alter session set NLS_DATE_FORMAT='dd.mm.yyyy';

-- works now
alter table t modify column c date ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文