How to conditionally remove the first two characters from a column

Viewed 5380

I have the below data of some phone records, and I want to remove the first two values from each record as they are a country code. What is the way by which I can do this using Scala, Spark, or Hive?

phone
|917799423934|
|019331224595|
|  8981251522|
|917271767899|

I'd like the result to be:

phone
|7799423934|
|9331224595|
|8981251522|
|7271767899|

How can we remove the prefix 91,01 from each record or each row of this column?

4 Answers

Phone size can be different, such construction can be used (Scala):

df.withColumn("phone", expr("substring(phone,3,length(phone)-2)"))

An improvement I believe, would prefer a list with contains or the equivalent of, but here goes:

import org.apache.spark.sql.functions._

case class Tel(telnum: String)
val ds = Seq(
     Tel("917799423934"),
     Tel("019331224595"),
     Tel("8981251522"),
     Tel("+4553")).toDS()

val ds2 = ds.withColumn("new_telnum", when(expr("substring(telnum,1,2)") === "91" || expr("substring(telnum,1,2)") === "01", expr("substring(telnum,3,length(telnum)-2)")).otherwise(col("telnum"))) 

ds2.show

returns:

+------------+----------+
|      telnum|new_telnum|
+------------+----------+
|917799423934|7799423934|
|019331224595|9331224595|
|  8981251522|8981251522|
|       +4553|     +4553|
+------------+----------+

We may need to think of the +, but nothing was stated.

Using regular expressions

Use regexp_replace (add more extension codes if necessary):

select regexp_replace(trim(phone),'^(91|01)','') as phone --removes leading 91, 01 and all leading and trailing spaces
from table;

The same using regexp_extract:

select regexp_extract(trim(phone),'^(91|01)?(\\d+)',2) as phone --removes leading and trailing spaces, extract numbers except first (91 or 01) 
from table;

If they are strings then for a Hive query:

sql("select substring(phone,3) from table").show
Related