Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL switch firstname and lastname

I am Sorry for question but I am stuck. How can I switch row where is lastname and first name = ‘Ware, John’ to get fullname without comma ‘John Ware’ ? Thank you very much

SELECT
    SUBSTR( NAME, 1, ( LOCATE( ',', NAME ) ) ) AS FIRSTTNAME,
    SUBSTR( NAME,    ( LOCATE( ',', NAME ) ) ) AS LASTNAME
FROM
    table

I am keep getting error:

No function matches the given name and argument types. You might need to add explicit type casts.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Names
Camerata Si, The
Zukerman, Pinchas
Phelps, Cynthia
Watts, Andre (André)

>Solution :

The REGEXP_REPLACE function offers one convenient approach here:

SELECT full_name,
       REGEXP_REPLACE(full_name, '([^,]+),\s*(.*)', '\2 \1') AS full_name_out
FROM yourTable;

Output:

full_name            | full_name_out
Camerata Si, The     | The Camerata Si
Zukerman, Pinchas    | Pinchas Zukerman
Phelps, Cynthia      | Cynthia Phelps
Watts, Andre (André) | Andre (André) Watts

Here is a running demo for Postgres.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading