SQL Hive 填充前导零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24999931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:19:13  来源:igfitidea点击:

Hive padding leading zeroes

sqlhivebigdata

提问by Muthu Palaniappan

I need the output of a string column in my table as 13 length char, irrespective of whatever length it is, i need to stuff the remaining chars with 0...

我需要将表中字符串列的输出作为 13 个长度的字符,无论它是什么长度,我都需要用 0 填充剩余的字符...

I tried to use the following code in my hive query, but failed to get the desired output

我尝试在 hive 查询中使用以下代码,但未能获得所需的输出

right('0000000000000' + ProductID, 13)

Any help? Thanks

有什么帮助吗?谢谢

回答by Jason Rosendale

Hive has built-in lpad and rpad functions. In your case you could use:

Hive 具有内置的 lpad 和 rpad 功能。在您的情况下,您可以使用:

lpad(ProductId, 13, "0")

Or, if you might need to truncate to 13 characters, you could wrap this in the "right" function.

或者,如果您可能需要截断为 13 个字符,则可以将其包装在“正确”函数中。