【问题标题】:Convert char to date - Hive将 char 转换为日期 - Hive
【发布时间】:2020-05-23 01:39:55
【问题描述】:

我需要在 1 列日期中连接 2 列字符。 我试过了:

INSERT INTO tb_teste PARTITION (dt_originacao_fcdr)
SELECT
tp_registro_fcdr,
seq_registro_fcdr,
tp_cdr_fcdr,
dt_atendimento_fcdr,
date_dt_atendimento_fcdr,
hr_atendimento_fcdr,
timestamp(from_unixtime(unix_timestamp(CONCAT(dt_atendimento_fcdr, hr_atendimento_fcdr), 'yyyyMMddHHmmss')), "yyyy-MM-dd HH:mm:ss") as date_hr_atendimento_fcdr,
duracao_atend_fcdr,
hr_originacao_fcdr,
duracao_total_fcdr,
duracao_chamada_tarifada_fcdr,
st_chamada_fcdr,
fim_sel_orig_fcdr,
numero_a_fcdr,
tp_numero_a_fcdr,
numero_b_fcdr,
tp_numero_b_fcdr,
numero_b_orig_fcdr,
numero_c_fcdr,
tp_numero_c_fcdr,
tp_trafego_fcdr,
esn_fcdr,
central_fcdr,
erb_fcdr,
tp_erb_fcdr,
face_erb_inici_fcdr,
erb_final_fcdr,
face_erb_final_fcdr,
erb_original_fcdr,
imsi_fcdr,
imei_fcdr,
tecnologia_fcdr,
cd_oper_ass_a_fcdr,
cd_oper_ass_b_fcdr,
cgi_fcdr,
nu_tlfn_fcdr,
tp_tlfn_fcdr,
tp_tarifa_fcdr,
ident_num_a_fcdr,
ident_num_b_fcdr,
cd_prestadora_fcdr,
cna_orig_ar_erb_fcdr
FROM tb_op_nor;

结果:date_hr_atendimento_fcdr 2019-03-03

包含时间和日期的列不为空或为空。 示例:

时区:巴西。

我需要同一列中的日期和时间。

【问题讨论】:

标签: sql date hive type-conversion


【解决方案1】:

您可以concat 两个字段然后unix_timestamp 并使用from_unixtimestamp 函数我们可以格式化输出时间戳。

with cte as (select stack(1,"20190303","131615") as (dt,hr)) --sample data
select 
timestamp( --cast to timestamp
        from_unixtime(unix_timestamp(concat(dt,hr),'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss") --concat and change the format
        ) 
from cte

Output:

2019-03-03 13:16:15.0

如果您想将巴西时间转换为 UTC,反之亦然,请使用 to_utc_timestamp/from_utc_timestamp

【讨论】:

  • 谢谢,舒。我试过这个:CAST(timestamp(from_unixtime(unix_timestamp(CONCAT(dt_atendimento_fcdr, hr_atendimento_fcdr), 'yyyyMMddHHmmss')), "yyyy-MM-dd HH:mm:ss")) as date_hr_atendimento_fcdr,但失败了:无关输入 ')' 在原始类型规范(状态=42000,代码=40000)中期望 AS 接近 'as'。你能帮帮我吗?
  • @LetíciaBoniottiFerreira,试一试timestamp(from_unixtime(unix_timestamp(CONCAT(dt_atendimento_fcdr, hr_atendimento_fcdr), 'yyyyMMddHHmmss')), "yyyy-MM-dd HH:mm:ss") as date_hr_atendimento_fcdr,timestamp() 表示我们在这里转换为时间戳!,无需保留 cast() 再次..
  • 现在,它的作品。但是,我的输出只是日期,没有时间。 date_hr_atendimento_fcdr -> 2019-03-03
  • @LetíciaBoniottiFerreira,您能否用您的选择语句和示例数据更新问题?
  • 好的。样本数据在问题中。
猜你喜欢
  • 2018-10-19
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多