Sql 在配置单元中如何将日期转换为MM/DD/YY格式
我已经创建了一个表并加载了数据,数据如下所示Sql 在配置单元中如何将日期转换为MM/DD/YY格式,sql,hive,hiveql,datetime-format,Sql,Hive,Hiveql,Datetime Format,我已经创建了一个表并加载了数据,数据如下所示 1904287 Christopher Rodriguez Jan 11, 2003 963915 Thomas Stewart 6/17/1969 2236067 John Nelson 08/22/54 对于最后一列,日期格式不同。当前它是配置单元中的string数据类型 我需要将第三列中的所有值更改为所需格式,如配置单元表中的MM/DD/YY格式。您的第三个日期对我来说无效,它可以有多个年份组合 CR
1904287 Christopher Rodriguez Jan 11, 2003
963915 Thomas Stewart 6/17/1969
2236067 John Nelson 08/22/54
对于最后一列,日期格式不同。当前它是配置单元中的string
数据类型
我需要将第三列中的所有值更改为所需格式,如配置单元表中的
MM/DD/YY
格式。您的第三个日期对我来说无效,它可以有多个年份组合
CREATE TABLE test_dev_db.datetable
(Id string,
Name string,
user_date string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS orc
;
INSERT INTO TABLE test_dev_db.datetable VALUES
('1904287', 'Christopher Rodriguez', 'Jan 11, 2003'),
('963915', 'Thomas Stewart', '6/17/1969'),
('2236067', 'John Nelson', '08/22/54');
set hive.cli.print.header=true;
select Id,Name,user_date,
CASE WHEN user_date rlike '[a-zA-Z]{3} [0-9]{2}, [0-9]{4}'
THEN
to_date(from_unixtime(UNIX_TIMESTAMP(user_date,'MMM dd,yyyy')))
WHEN user_date rlike '[0-9]{1}/[0-9]{2}/[0-9]{4}'
THEN
to_date(from_unixtime(UNIX_TIMESTAMP(user_date,'MM/dd/yyyy')))
WHEN user_date rlike '[0-9]{2}/[0-9]{2}/[0-9]{2}'
THEN
to_date(from_unixtime(UNIX_TIMESTAMP('08/22/54','MM/dd/yyyy')))
end as format_date
from test_dev_db.datetable;
id name user_date format_date
1904287 Christopher Rodriguez Jan 11, 2003 2003-01-11
963915 Thomas Stewart 6/17/1969 1969-06-17
2236067 John Nelson 08/22/54 0054-08-22
Time taken: 0.467 seconds, Fetched: 3 row(s)
@拉维-你得到你问题的答案了吗?