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

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

Related

Concatenating Full Names in Microsoft SQL Server

Suppose I have a table with the following information
Title FirstName MiddleName LastName Suffix
======|============|=============|============|======
Mr. John Fitzgerald Kennedy NULL
NULL John NULL Doe III
I want to create a query to output the full name of the above information. For example, I want the output to look like
Mr. John Fitzgerald Kennedy
John Doe, III
I was thinking of using the concat() function, but I am unsure how to go about this to ensure I don't have extra spaces or hanging commas in the names.
This is a little tricky. SQL Server 2017+ supports concat_ws(). However, you still need a comma for the suffix. Forturnately, concat() ignores NULLs:
select concat(concat_ws(' ', title, firstname, middlename, lastname),
', ' + suffix
)
Earlier versions require a bit more work to avoid multiple spaces when there are NULL values:
select ltrim(concat(' ' + title,
' ' + firstname,
' ' + middlename,
' ' + lastname,
', ' + suffix
)
)
This uses the fact that + returns NULL when an argument is NULL. But, concat() ignores it.
Note: Neither probably does what you want if all the names are NULL but the suffix is present. However, I doubt that is an issue in your data.
Here is a db<>fiddle.

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

Spilt Name field in SQL into First Name and Last Name

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.

How to parse a row in SQL into columns (separator-spaces)

I need to parse row (with spaces) into two different columns in SQL. The rows look like that:
FullName
John Mayer
Rajon Jennings
Donna Fraye
Michael Moore
So the expected result is :
Name
John
Rajon
Donna
Michael
Surname
Mayer
Jennings
Fraye
Moore
How can i do that in SQL?
If you have a requirement like First Name should be string before first space and rest everything what follows should go as Last name , you can update two columns as:
Update T
Set T.FirstName = Left(ltrim(rtrim(FullName)), CHARINDEX(' ',ltrim(rtrim(FullName))))
,T.LastName = Right(ltrim(rtrim(FullName)),len(FullName)- CHARINDEX(' ',ltrim(rtrim(FullName))))
From Test T
Check Demo here..
You can use a split function.This function must be created.
Here are some examples :
http://ole.michelsen.dk/blog/split-string-to-table-using-transact-sql/
Split function by comma in SQL Server 2008
If there is only ever 1 space / 2 name and you don't have to cater for names like Vincent van Gough, then a combination of substring and charindex should do.
select substring(fullname, 1, charindex(' ', fullname)) as name,
substring(fullname, charindex(' ', fullname)+1, len(fullname)-charindex(' ', fullname)) as surname
from tbl
This does not cover people with the same first and last name (ie. john john) but will work for rest. Not recommended but is a different solution.
select left(fullname, charindex(' ',fullname)) Name,
replace(fullname,left(fullname, charindex(' ',fullname)),'') Surname
from names
Fiddle

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