Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql - how to split fullname into first and last name

Tags:

tsql

If I have a table with a column that contains fullnames such as :

fullname
------------
Joe Bloggs
Peter Smith
Mary Jones and Liz Stone

How can I retried the first and last name from each of the entries in the full name column using SQL. I'm not worried about the second name in the 3rd entry in my example i.e. Liz Stone.

So basically to retrieve

Firstname
---------
Joe
Peter
Mary

Lastname  
--------
Bloggs 
Smith
Jones
like image 436
thegunner Avatar asked Jan 04 '17 11:01

thegunner


People also ask

How do I separate a first and last name in SQL?

In using the PARSENAME function to split the full name, we need to replace the space with a period (REPLACE(@FullName, ' ', '. ')). Then to get the first name, we pass a value of 2 to the PARSENAME string function, which corresponds to the [Owner Name].

How do you split first name middle name and last name in SQL?

Now write below SQL query: select substr(name, 1, (instr(name, “ “, 1,1) - 1)) as fname, substr(name, (instr(name, “ “, 1,1) + 1), (instr(name, “ “, 1, 2) -1)) as middlename, substr(name, (instr(name, “ “, 1, 2) + 1) ) as lame from tablename; Let me know it is working for you.

How do I separate two words in SQL?

The STRING_SPLIT(string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT() function and ' ' as the second argument.


3 Answers

Here is a pre SQL Server 2016 method, which uses basic string functions to isolate the first and last names.

SELECT SUBSTRING(fullname, 1, CHARINDEX(' ', fullname) - 1) AS Firstname,     
       SUBSTRING(fullname,
                 CHARINDEX(' ', fullname) + 1,
                 LEN(fullname) - CHARINDEX(' ', fullname)) AS Lastname
FROM yourTable

Note that this solution assumes that the fullname column only contains a single first name and a single last name (i.e. no middle names, initials, etc.).

like image 54
Tim Biegeleisen Avatar answered Oct 10 '22 15:10

Tim Biegeleisen


I use this query to retrive first and lastname

SELECT SUBSTRING(FULLNAME, 1, CASE WHEN CHARINDEX(' ', FULLNAME)>0 THEN CHARINDEX(' ', FULLNAME) - 1 ELSE LEN(FULLNAME) END ) AS Firstname,
  REVERSE(SUBSTRING(REVERSE(FULLNAME), 1, CASE WHEN CHARINDEX(' ', REVERSE(FULLNAME))>0 THEN CHARINDEX(' ', REVERSE(FULLNAME)) - 1 ELSE LEN(REVERSE(FULLNAME)) END ) )AS Firstname FROM  HRMDESFO.EMPLOID

Resuls

like image 20
Luis Pinheiro Avatar answered Oct 10 '22 14:10

Luis Pinheiro


SELECT CASE
           WHEN CHARINDEX(' ', FullName) > 0
           THEN SUBSTRING(FullName, 1, LEN(FullName) - CHARINDEX(' ', REVERSE(FullName)))
           ELSE ''
       END AS FirstName,
       CASE
           WHEN CHARINDEX(' ', FullName) > 0
           THEN REVERSE(SUBSTRING(REVERSE(FullName), 
                       1, 
                       CHARINDEX(' ', REVERSE(FullName)) - 1))
           ELSE FullName
       END AS LastName
FROM(VALUES('Mary Anne Bloggs'), ('Joe Bloggs'), ('Bloggs')) AS T(FullName);

This version checks that there is a space in the full name to split on. If there isn't then the first name is set to an empty string and the full name is put into the surname. Also, reverse is employed to split on the last space when there is more than one space

like image 31
Colin Avatar answered Oct 10 '22 16:10

Colin