【问题标题】:Hive converting int column to string and to the target type: dateHive 将 int 列转换为字符串和目标类型:日期
【发布时间】:2021-12-18 13:36:12
【问题描述】:

我在 hiveql 中转换数据类型时遇到问题。列的数据类型是int,应该转换成date,但是没有int到date的直接转换函数。起初我将 int 转换为字符串,它的工作原理如下:

从现在开始,我基于将数据转换为字符串的第二列。当我尝试使用 cast(string as date) 时,第三列只返回空值:

有趣的是,当要转换的字符串值是用户手动输入的时候没有问题:

你们中有人知道如何处理这个问题吗?

【问题讨论】:

  • SELECT cda_date, cast(concat_ws('-', substr(cda_date,0,4),substr(cda_date,5,2),substr(cda_date,7,2)) 作为日期) FROM database_name.table_name 去
  • 如上解决。

标签: sql types hive


【解决方案1】:

你在评论中的方法很不错。

另一种使用 regexp_replace 的方法:

select date(regexp_replace(cda_date,'^(\\d{4})(\\d{2})(\\d{2})','$1-$2-$3'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 2023-04-07
    • 2013-06-12
    相关资源
    最近更新 更多