Hive padding leading zeroes

Muthu Palaniappan picture Muthu Palaniappan · Jul 28, 2014 · Viewed 29.6k times · Source

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...

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

right('0000000000000' + ProductID, 13)

Any help? Thanks

Answer

Jason Rosendale picture Jason Rosendale · Jul 28, 2014

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

lpad(ProductId, 13, "0")

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