Reputation: 21
I have a string AF1234
and I want to print 1234
, also sometimes I get only numbers like 25
and 23
and I want these numbers as well, hence I am trying to use a regex_replace to check the string, below is the syntax:
select substring(nvl(nullif(regexp_replace(regexp_replace('AF1234','A$',''),'[[:digit:]]',''),''),'XX'),1,2)
This works in impala but not hive
Upvotes: 1
Views: 439
Reputation: 1023
If you want numbers out of alphanumeric try this,
scala> spark.sql("select regexp_replace('AF1234','[A-Z]*','')").show
+------------------------------+
|regexp_replace(AF1234, [A-Z]*, )|
+------------------------------+
| 1234|
+------------------------------+
spark.sql("select regexp_replace('23','[A-Z]*','')").show
+----------------------------+
|regexp_replace(23, [A-Z]*, )|
+----------------------------+
| 23|
+----------------------------+
Upvotes: 0
Reputation: 38290
Tested in Hive, Impala, Spark SQL
select regexp_extract('AF1234','(\\d+)',1)
Result:
1234
Upvotes: 1