Skip to main content
chiranjeevivenk
Gold ⭐️
Jan 17, 2019 5:02 AM UTC
Solved

How do I remove currency symbol from string and convert into int in hive? EXAMPLE (124$ INTO 124)

How do I remove a currency symbol from a string and convert it into an int in Hive? For example, how do I change '124$' into 124?

Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
Jan 17, 2019 6:49 AM UTC
@Venkat

Use regexp_replace function in hive to replace $ with '' then cast to int.

Example:

select int(regexp_replace(string("124$"),'\\$',''));
+------+--+
| _c0 |
+------+--+
| 124 |
+------+--+

(or)

Starting from Hive-1.3 version use replace function.

select int(replace(string("124$"),'$',''));
+------+--+
| _c0 |
+------+--+
| 124 |
+------+--+

1 reply

Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
Jan 17, 2019 6:49 AM UTC
@Venkat

Use regexp_replace function in hive to replace $ with '' then cast to int.

Example:

select int(regexp_replace(string("124$"),'\\$',''));
+------+--+
| _c0 |
+------+--+
| 124 |
+------+--+

(or)

Starting from Hive-1.3 version use replace function.

select int(replace(string("124$"),'$',''));
+------+--+
| _c0 |
+------+--+
| 124 |
+------+--+

Translate