【问题标题】:Merging two columns into a single column and formatting the content to form an accurate date-time format in Hive?将两列合并为一列并格式化内容以在 Hive 中形成准确的日期时间格式?
【发布时间】:2015-06-16 17:05:54
【问题描述】:

这些是 2 列(月、年)。我想从中创建一个具有准确日期时间格式的列('YYYY-MM-DD HH:MM:SS'),并在表中添加为新列。

 Month     year
 12/ 3   2013 at 8:40pm
 12/ 3   2013 at 8:39pm
 12/ 3   2013 at 8:39pm
 12/ 3   2013 at 8:38pm
 12/ 3   2013 at 8:37pm

同样的最佳配置单元查询可能是什么?我无法形成一个准确的正则表达式。

【问题讨论】:

    标签: sql regex hadoop hive datetime-format


    【解决方案1】:

    我将假设 12 是 month,而 3 是 day,因为您没有指定。另外,您说您想要HH:MM:SS,但您的示例中没有秒数,所以我不知道您将如何将它们放入其中。在您的示例中,我还将8:37pm 更改为8:37am 以尝试这两种情况。

    查询:

      select concat_ws(' ', concat_ws('-', yr, month, day)
                          , concat_ws(':', hour, minutes)) date_time
      from (
        select yr
          , case when length(month) < 2 then concat('0', month) else month end as month
          , case when length(day) < 2 then concat('0', day) else day end as day
          , case when instr(minutes, 'pm') > 0 then cast(hour+12 as int)
                 when instr(minutes, 'am') > 0 and length(hour) < 2 then concat('0', hour)
                 else hour end as hour
          , substr(minutes, 1, 2) minutes
        from (
        select ltrim(split(Month, '\\/')[1]) day
          , split(Month, '\\/')[0] month
          , split(year, ' ')[0] yr
          , split(split(year, ' ')[2], '\\:')[0] hour
          , split(split(year, ' ')[2], '\\:')[1] minutes
        from test.sample_data ) x ) y
    

    输出:

    date_time
    
    2013-12-03 20:40
    2013-12-03 20:39
    2013-12-03 20:39
    2013-12-03 20:38
    2013-12-03 08:37
    

    【讨论】:

    • 您好,查询看起来不错,但您是否尝试在您的系统上运行它?因为在我的情况下,它会抛出一些不同的输出。 OK - 12-03 at - 12-03 at - 12-03 at - 12-03 at - 12-03 at 所用时间:0.245 秒,获取:5 行 hive>
    【解决方案2】:

    我假设 12 是一个月和 3 天。在 sql server 2008 中尝试这个

    select convert(datetime,REPLACE(Month+'/'+year,'at',''))
    

    【讨论】:

    • 这不是 sql server 2008 问题
    【解决方案3】:

    是的,工作正常,非常感谢@GoBrewers14。你拯救了我的一天!

    有点不匹配,这是正确的:

    select concat_ws(' ', concat_ws('-', yr, month, day), concat_ws(':', hour, minutes)) date_time
      from (
        select yr
          , case when length(month) < 2 then concat('0', month) else month end as month
          , case when length(day) < 2 then concat('0', day) else day end as day
          , case when instr(minutes, 'pm') > 0 then cast(hour+12 as int)
                 when instr(minutes, 'am') > 0 and length(hour) < 2 then concat('0', hour) else hour end as hour
          , substr(minutes, 1, 2) minutes
                from (
                select ltrim(split(month, '\\/')[1]) day
                  , ltrim(split(month, '\\/')[0]) month
                  , split(year, ' ')[1] yr
                  , split(split(year, ' ')[3], '\\:')[0] hour
                  , split(split(year, ' ')[3], '\\:')[1] minutes
                from db.table_name )
                     x ) y limit 5;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 2021-07-14
      相关资源
      最近更新 更多