Splitting FullName Column - sql

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)

Related

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

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

how to split a column into multiple value using sql

If I have thousands of rows with data and I would like to find out if the column called "Last
Name " contains both First Name and Last Name (could be middle initial too). What SQL command
do I use ? (SQL Server 2008). Once I find it, how do I split the Last Name field and place
first name or middle initial in their own columns ?
For example:
First Name MI Last Name
John B. Smith
Karen A. Jones
Jane Lawrence
Joan K. Bates
Could it be done in Excel as well ?
You could look for spaces in the last name:
select *
from t
where lastname like '% %';
You could look for rows where the first name is missing:
select *
from t
where firstname is null or firstname = '';
EDIT:
If we assume that the names are "simple" as given in the sample data, you can do:
update t
set firstname = left(lastname, charindex(' ', lastname) - 1),
lastname = right(lastname, charindex(' ', reverse(lastname))),
mi = (case when lastname like '% % %'
then rtrim(ltrim(substring(lastname, charindex(' ', lastname + 1), 2)))
end)
where lastname like '% %' and firstname is null;
This makes lots of assumptions about the formats . . . that there are no complete middle names, that there are no honorics, that there are no suffixes, that there are no multiple spaces together. But if your data is simple, it should work.
Here is an example of the logic in SQL Fiddle.
I would do it in two passes:
-- Set first name
UPDATE names
SET FirstName = SUBSTR(LastName,1,CHARINDEX(' ',LastName) - 1),
LastName = SUBSTR(LastName,CHARINDEX(' ',LastName) + 1, LEN(LastName))
WHERE FirstNAme IS NULL
AND CHARINDEX(' ',LastName) > 0
-- Set middle name
UPDATE names
SET MI = SUBSTR(LastName,1,CHARINDEX(' ',LastName) - 1),
LastName = SUBSTR(LastName,CHARINDEX(' ',LastName) + 1, LEN(LastName))
WHERE MI IS NULL
AND CHARINDEX(' ',LastName) > 0
You may have a few false positives but that should get the vast majority of cases.
I would STRONGLY recommend creating a transaction, doing the updates, doing a SELECT to see the results and rolling back the transaction since the update is not reversible.

Select only when the field has more than one word

SELECT name FROM clients;
Table clients
id | name |
1 John
2 John Bravo
3 John Alves
4 Jo
In postgres, how can I select only names with more than one word? For example. This should be the output:
John Bravo
John ALves
I think just test if the name contains a space: where name like '% %'
But this will give you some problem if you name can contain space char befor or after the name, like: ' JOHN' or 'Luck '
If word means to you a space delimited token, then the following would do the trick:
SELECT name FROM clients WHERE name LIKE '% %';
But this will also give you those that have empty names made out of spaces only. Also performance-wise, this will be a costly query.
First you may have to find the number of words in the column, then only select where the number of words is greater than 1.
For example:
SELECT LENGTH(name) - LENGTH(REPLACE(name , ' ', ''))+1 FROM clients;
This will return the number of words in each row, which we have in the field name.
Then you can proceed putting this in a nested select SQL statement something like :
SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 as mycheck, name FROM clients where (SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1)>1
This will return only where words are greater than 1 (>1). Else you can user >2 to return columns with more than 2 words.
LIKE is an option, or you can use split_part:
SELECT name FROM clients WHERE split_part(trim(name), ' ', 2) <> ''