简体   繁体   English

提取 Hive SQL 中两个字符之间的数字

[英]Extract number between two characters in Hive SQL

The query below outputs 1642575.0 .下面的查询输出1642575.0 But I only want 1642575 (just the number without the decimal and the zero following it).但我只想要1642575 (只是没有小数点的数字和后面的零)。 The number of delimited values in the field varies.字段中分隔值的数量会有所不同。 The only constant is that there's always only one number with a decimal.唯一不变的是总是只有一个带小数的数字。 I was trying to write a regexp function to extract the number between " and . .我试图编写一个正则表达式 function 来提取".之间的数字。

How would I revise my regexp_extract function to get the desired output?我将如何修改我的 regexp_extract function 以获得所需的 output? Thank you!谢谢!

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

You can cast the result to bigint .您可以将结果转换为bigint

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

You can use round if you want to round it off.如果你想四舍五入,你可以使用round。

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

Use this regexp: '"(\\d+)\\.'使用这个正则表达式: '"(\\d+)\\.' - means double-quote, capturing group with one or more digits, dot. - 表示双引号,用一个或多个数字捕获组,点。

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

Result:结果:

1642575

To skip any number of leading zeroes, use this regexp: '"0*(\\d+)\\.'要跳过任意数量的前导零,请使用此正则表达式: '"0*(\\d+)\\.'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM