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