Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive padding leading zeroes

Tags:

sql

hive

bigdata

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

like image 697
Muthu Palaniappan Avatar asked Jul 28 '14 16:07

Muthu Palaniappan


People also ask

What is Lpad in Hive?

The LPAD function returns the string with a length of len characters left-padded with pad. Example: LPAD('hive',6,'v') returns 'vvhive' LTRIM( string str ) The LTRIM function removes all the trailing spaces from the string.

How do I remove spaces between words in Hive?

Use regexp_replace to replace consecutive multiple spaces with one space character.

How do I trim in Hive?

Trim function removes the extra space from both ends of the string value. In other words, it removes the leading and trailing space from the string. Along with this, Hive providing two more trim function as below. Ltrim – It return a string after trimming the space from the beginning (left hand side) of a string.

How do I concatenate in Hive?

The || operator can be used to join the strings together. It is a concatenation operator. Similar to concat() function, we can use this operator to add the strings together in Hive.


1 Answers

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.

like image 125
Jason Rosendale Avatar answered Nov 04 '22 14:11

Jason Rosendale