Spilt Name field in SQL into First Name and Last Name - sql

I have a column called name that stores the entire name. I want to split it into First Name and Last Name in a Select statement. The problem is that the name might be stored like:
Mike Joe or Mike & Sarah Smith.
If it is only two names (first and last) I can use this code:
, substring(name, CHARINDEX(' ', name)+1, len(name)-(CHARINDEX(' ', name)-1)) as [LastName]
, left(name, CHARINDEX(' ', name)) as [FirstName]
But if the name is "Mike & Sarah Smith" I want it to be like:
First Name: Mike & Sarah
Last Name: Smith
How do I modify the code above to handle both situations?

If the last part is always the last name then you can use reverse function with right and left functions:
select
left('Mike & Sarah Smith',
len('Mike & Sarah Smith')-charindex(' ',reverse('Mike & Sarah Smith'))) FirstName,
right('Mike & Sarah Smith',charindex(' ',reverse('Mike & Sarah Smith'))) LastName
Output:
FirstName LastName
Mike & Sarah Smith
just put the name of your field instead of 'Mike & Sarah Smith' in the code when using it for the query.

You can do this by looking for the last space in a name using CHARINDEX() coupled with REVERSE():
SELECT RIGHT(name,CHARINDEX(' ',REVERSE(name))-1) AS LastName
,LEFT(#name,LEN(name) - CHARINDEX(' ',REVERSE(name))-1) AS FirstName
Names without spaces will present issues, so you may need to add a CASE expression to the above to ensure they have a space.

Related

Splitting Full Name into First Name and Last Name : (Redshift)

I have a table below with names of the customer
fullname
Ash Ketchum
Mary Joe Keth
John
I want to split it into first and last names
firstname lastname
Ash Ketchum
Mary Joe Keth
John
I referred to this post and tried all the solution but nothing seems to work for me. I already did the same exercise in SQLServer using CHARINDEX() but seems like it's not supported in redshift
, same is the case with SUBSTR()
Simplest way to split human name in postgres?
You should be able to use SPLIT_PART():
SPLIT_PART(fullname, ' ', 1) as first_name,
SPLIT_PART(fullname, ' ', 2) as last_name

Splitting FullName Column

The column Staff_Name lists the name as "Doe, John A".
I need this to be separated into 2 columns like below:
Last Name | First Name
Doe | John A
Heres the code I have but I need it to remove the "," that it outputs
LTRIM(RTRIM(SUBSTRING(Staff_Name, 0, CHARINDEX(' ', Staff_Name)))) As [Physician First Name],
LTRIM(RTRIM(SUBSTRING(Staff_Name, CHARINDEX(' ', Staff_Name)+1, 8000)))As [Physician Last Name]
Here is what the code is currently outputting:
Last Name | First Name
Doe, | John A
I think you can use , instead of space to remove the comma.
CHARINDEX(',', Staff_Name)

Split field (FirstName) after space

I have a column of First Names, however, both first name and middle name are combined in this column. For ex the column name is FirstName and contains Jim JR. next to this column I have a MiddleName column, which is currently blank. I want to split the JR. after Jim from the FirstName column and into the MiddleName.
If they are separated by space, you can use the CHARINDEX(' ',FirstName) and SUBSTRING(). like:
SUBSTRING(FirstName,
CHARINDEX(' ',FirstName),
LEN(FirstName)-CHARINDEX(' ',FirstName))

Parsing full name in SQL Server 2008

I know this is a common question and I've reads all the posts just cannot get it to work so here I am. I have a table called PUBLICDATA. In it a table called PEOPLE and a column called NAME.
In the names column are strings of names formatted like:
smith, steve
smith steve a
smith, steve Andrew
smith, steve Andrew robin
What I'd like is a quick and dirty script that I can run that will parse the string in the NAME column and dump the split names into the FIRSTNAME, MIDDLENAME AND LASTNAME columns that also reside in the NAMES table.
ALSO... I have another table (let's say everything is named the same, however the names are like this:
steve smith
steve a smith
steve Andrew smith jr
steve Andrew jackson smith
Both tables do not have salutation and some have Jr, SR, etc... thank you in advance everyone...
PS.. please no self contained examples as I've seen them around but cant get them to work with my situation :(
Well you need a split function. I would split the names using this suggestion: https://gallery.technet.microsoft.com/scriptcenter/T-SQL-Script-to-Split-a-308206f3 and put all of the names in their own table.
I would then recommend that if your data really is always structured last name firstname etc that you match on the length of the last name first. So you take:
SELECT name from your_table WHERE Left(name, Length(last_name_from_name_table))=Length(lastname_from_name_table) AND UPPER(Left(name_from_your_table, length(lastname_from_name_table))=UPPER(lastname_from_name_table)
and blow that out into a series of cases that capture the names you want with cases for first names, jr, etc.
This works like a charm
SELECT LEFT(Name, CHARINDEX(' ', Name)) AS FirstName, CASE WHEN CHARINDEX(' ', Name) <> LEN(Name) - CHARINDEX(' ', REVERSE(Name)) + 1 THEN SUBSTRING(Name, CHARINDEX(' ', Name)+ 1, LEN(Name) - CHARINDEX(' ', REVERSE(Name))-CHARINDEX(' ', Name)) end as middle, RIGHT( Name, CHARINDEX(' ', REVERSE(Name))) AS LastName from a01_parse_test

Combining Columns on Blank Rows

I have a question about the possibility of combining columns based on blanks ('') in a row. The reason behind this is because I am using a charindex to strip off the middle initial in the first name. However, if the data does not contain a middle initial I have it set to return a blank ('').
Here is an example of what I am looking to do.
First_Name First_Name2 Column_Needed
John B John John
Fred Fred
Mary D Mary Mary
Mike Mike
Scott S Scott Scott
I would like to have a third column that would combine the two columns as one column with no blanks and no middle initials but all first names.
Some example of the code I am using to strip middle initial...
LEFT([First_Name], CHARINDEX(' ', [First_Name])) AS [First_Name2]
You need to use a CASE statement in SQL:
SELECT CASE LEFT([First_Name], CHARINDEX(' ', [First_Name])) WHEN '' THEN [First_Name] ELSE LEFT([First_Name], CHARINDEX(' ', [First_Name])) END as Column_Needed
#Joseph B's is a much better answer.
You can use the COALESCE function to get the First Name without the middle initial, as below:
SELECT COALESCE([First_Name2], [First_Name]) as Column_Needed