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

Oracle SQL Developer – SUBSTR and LIKE

I have a column NAME_SURNAME (made of names and surnames) from a table people:

John Smith
Tim Burton
Harry Potter

I need to write a query to obtain only the names:

John
Tim
Harry

I am trying in this way nad it doesn’t work:

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

select NAME_SURNAME, substr(NAME_SURNAME, 1, LIKE '% %')
from PEOPLE

I don’t figure out how to use LIKE in this exercise.

Thank you really much

>Solution :

Use INSTR with SUBSTR to take a substring up to the first space:

SELECT NAME_SURNAME, SUBSTR(NAME_SURNAME, 1, INSTR(NAME_SURNAME, ' ') - 1)
FROM PEOPLE;

Demo

For a regex approach, we can use REGEXP_SUBSTR:

SELECT NAME_SURNAME, REGEXP_SUBSTR(NAME_SURNAME, '^\S+') AS FIRST_NAME
FROM PEOPLE;
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