How to select only people with a first name

Asked

Viewed 273 times

-4

I would like to know how to select people who have only their first name, that is, they have no last name.

Example:

Nome
----------
Leonardo Roberto
Ana
Júlio
Maria Ana

Expected result:

Ana, Júlio

  • 2

    Welcome to SOPT. Would you like [Edit] your post and add the code you are using, or what have you tried to do so far? So we can analyze and suggest a change. See the [Ask] page for help in clarifying this question.

2 answers

2

Although your question is unclear, I think it will be simple to reach a solution.
Let’s assume you have the table below (very basic example):

CREATE TABLE Pessoas
(
    Nome VARCHAR(255)
)

Table containing the following information:

INSERT INTO Pessoas VALUES('Leonardo Roberto')
INSERT INTO Pessoas VALUES('Ana')
INSERT INTO Pessoas VALUES('Júlio')
INSERT INTO Pessoas VALUES('Maria Ana')

To get names that don’t have a surname, just look for those that don’t have spaces:

SELECT  * 
FROM    Pessoas
WHERE   STRPOS(LTRIM(RTRIM(Nome)), ' ') = 0

0

An alternative would be a call to the function POSITION(); whereas the surname will always be separated by a space (), can use the following:

SELECT *
FROM pessoa
WHERE POSITION(' ' in nome) > 0;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.