[FIXED] How to split the fullname column value into 3 columns and remove parenthesis between data using spark SQL

Issue

I am trying to split full name into three columns, remove the value in parenthesis and star sign as fullname,First Name and Last Name Using spark SQL. I tried with Regex and split but no avail. Can any one help me with the code snippet. example:

Fullname
Nayaka,Malya (Nayaka)
Toyoda,Masayuki *Toyoda *
Suzy,Thamas *FUNCTIONAL *

Expected output:

Fullname Firstname Lastname Fullname
Nayaka,Malya (Nayaka) Malya Nayaka Nayaka.Malya
Toyoda,Masayuki *Toyoda * Masayuki Toyoda Toyoda,Masayuki
Suzy,Thamas *FUNCTIONAL * Thamas Suzy Suzy,Thamas

Solution

This should do the trick:

spark.sql(""" select    split ( ltrim(rtrim(regexp_replace(split(fullname, ',')[1], '[^a-zA-Z ]', ''))), ' ')[0] as FIRSTNAME,
                        ltrim(rtrim(regexp_replace(split(fullname, ',')[0], '[^a-zA-Z ]', ''))) as LASTNAME from employee   """).show(false)


Answered By - thebluephantom
Answer Checked By - Mildred Charles (sql.easybugfix Admin)

Related Posts

Post a Comment