【问题标题】:Extract number between two characters in Hive SQL提取 Hive SQL 中两个字符之间的数字
【发布时间】:2021-06-28 01:21:30
【问题描述】:

下面的查询输出1642575.0。但我只想要1642575(只是没有小数点的数字和后面的零)。字段中分隔值的数量会有所不同。唯一不变的是总是只有一个带小数的数字。我试图编写一个正则表达式函数来提取". 之间的数字。

如何修改我的 regexp_extract 函数以获得所需的输出?谢谢!

select regexp_extract('{"1244644": "1642575.0", "1338410": "1650435"}','([1-9][0-9]*[.][0-9]+)&*');

【问题讨论】:

    标签: sql regex hive hiveql numeric


    【解决方案1】:

    您可以将结果转换为bigint

    select cast(regexp_extract('{"1244644": "1642575.9", "1338410": "1650435"}','([1-9][0-9]*[.][0-9]+)&*') as bigint) col;
    output - 1642575
    

    如果你想四舍五入,你可以使用round。

    select round(regexp_extract('{"1244644": "1642575.9", "1338410": "1650435"}','([1-9][0-9]*[.][0-9]+)&*')) col;
    output - 1642576
    

    【讨论】:

      【解决方案2】:

      使用这个正则表达式:'"(\\d+)\\.' - 表示双引号,用一个或多个数字捕获组,点。

      select regexp_extract('{"1244644": "1642575.9", "1338410": "1650435"}','"(\\d+)\\.',1)
      

      结果:

      1642575
      

      要跳过任意数量的前导零,请使用此正则表达式:'"0*(\\d+)\\.'

      【讨论】:

        猜你喜欢
        • 2018-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-05
        • 2014-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多