Change order of name column in oracle

207 Views Asked by At

I have a table customer with a column as 'Name' with customer names as its value. One example of that column value is 'Aditya, Narayan'. But I want the name to be 'Narayan, Aditya'. How to do that in an oracle SQL developer query? P.S. I cannot paste the image of the table as company policy.

1

There are 1 best solutions below

0
Littlefoot On

Two simple options include substr + instr combination (new_name_1) or regular expressions (new_name_2 and new_name_3).

SQL> with customer (name) as
  2    (select 'Aditya,Narayan' from dual union all
  3     select 'Little,Foot'    from dual
  4    )
  5  select name,
  6    --
  7    substr(name, instr(name, ',') + 1) ||','|| substr(name, 1, instr(name, ',') - 1) new_name_1,
  8    --
  9    regexp_substr(name, '\w+$') ||','|| regexp_substr(name, '^\w+') new_name_2,
 10    --
 11    regexp_replace(name, '(.*),(.*)', '\2,\1') new_name_3
 12  from customer;

NAME           NEW_NAME_1           NEW_NAME_2           NEW_NAME_3
-------------- -------------------- -------------------- --------------------
Aditya,Narayan Narayan,Aditya       Narayan,Aditya       Narayan,Aditya
Little,Foot    Foot,Little          Foot,Little          Foot,Little

SQL>