Combining Columns on Blank Rows - sql

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

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

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))

Adding comma after first word in column

I have a table called people and a column called NAME which has last first middle (if exists) . so SMITH JOHN J I'd like to add a comma after first word so it updates to SMITH, JOHN J
I tried running this but it blew up:
update people
set name = (CHARINDEX(' ', 0), 0, ',')
I know I'm close but it's eluding me :(
You can use STUFF() along with CHARINDEX() and LEFT() for this:
update people
set name = STUFF(name,1,CHARINDEX(' ',name )-1,LEFT(name ,CHARINDEX(' ',name )-1)+', ')
WHERE CHARINDEX(' ',name) > 0
Might add a WHERE to ensure there is a space in the name so it doesn't error, or a CASE expression.
Could also use REPLACE() with CHARINDEX() and LEFT():
REPLACE(name,LEFT(name,CHARINDEX(' ',name)-1),LEFT(name,CHARINDEX(' ',name)-1)+',')

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

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