parsing first and last name from fullname column - sql

Below is the data I am trying to manipulate. I am trying to get the last name and first name before the second comma
This is the code I currently have:
SELECT
[SDN_Name],
CASE WHEN CHARINDEX(', ', [SDN_Name]) > 0 THEN
LEFT([SDN_Name],(CHARINDEX(', ',[SDN_Name],1)-1))
ELSE
[SDN_Name]
END as LName,
CASE WHEN CHARINDEX(', ', [SDN_Name]) > 0 THEN
SUBSTRING([SDN_Name],CHARINDEX(', ',[SDN_Name])+1, ( LEN([SDN_Name]) - CHARINDEX(', ',[SDN_Name])+1))
ELSE
NULL
END as FName
FROM [Staging].[OFAC].[TP1]
Where ([SDN_Type] = 'individual')
For instance, if the full name is DELOS REYES, Feliciano , Jr.
I want DELOS REYES as the LName
and Feliciano Semborio as the Fname
but I don't want to capture ", Jr."
I'm able to parse the last name and everything right of the first comma.
How do I write a CASEstatment that will capture the first name without the suffix of the name?

Those are commas in SDN_Name field, right? Just use the CharIndex of ', ' instead of ' ', and it should work. Like this:
SELECT [SDN_Name],
CASE WHEN CHARINDEX(', ', [SDN_Name]) > 0 THEN
LEFT([SDN_Name],CHARINDEX(', ',[SDN_Name])-1)
ELSE
[SDN_Name]
END as LName,
CASE WHEN CHARINDEX(', ', [SDN_Name]) > 0 THEN
SUBSTRING([SDN_Name], CHARINDEX(', ',[SDN_Name])+2,
CASE WHEN RIGHT([SDN_Name], 1) = '.' THEN LEN([SDN_Name]-2) ELSE LEN([SDN_Name]) END
- (2+CHARINDEX(', ',[SDN_NAME],1))
)
ELSE
NULL
END as FName

Related

SQL to Take Last Name, First Name and MI and just return First and Last Name

I have searched Stack and getting close to what I need, but can't seem to figure out why when I have a person with just Last Name, First Name that the first name ends up as the Middle Initial? I am only needing the Last Name and First name to display it as First and Last name.
Sample Data:
EMP_ID
EMP_NAME
1234
JONES, JAMES R
5687
SMITH, BILL
What I want to end up with is:
EMP_ID
EMP_NAME_FULL
1234
JAMES JONES
5687
BILL SMITH
I am working with this code and once I can figure out how to resolve getting the first name to work, I planned to combine the First and Last Name substring/Parsing to one name.
SELECT DISTINCT
EMP_ID
,EMP_NAME
,SUBSTRING(EMP_NAME, 1, CHARINDEX(',', EMP_NAME) - 1) AS LASTNAME
,CASE WHEN PARSENAME(REPLACE(EMP_NAME, ',', '.'),1) LIKE '% %' THEN PARSENAME(REPLACE(PARSENAME(REPLACE(EMP_NAME, ',', '.'),1), ' ', '.'),2) ELSE PARSENAME(REPLACE(EMP_NAME, ',', '.'),1) END FIRSTNAME
,CASE WHEN PARSENAME(REPLACE(EMP_NAME, ' ', '.'),1) LIKE '%,%' THEN NULL ELSE PARSENAME(REPLACE(EMP_NAME, ' ', '.'),1) END MI
FROM EMP_TABLE
Try this:
[MYSQL]
select emp_name,
SUBSTRING_INDEX(SUBSTRING_INDEX(emp_name, ', ', -1), ' ', 1),
SUBSTRING_INDEX(emp_name, ',', 1)
from EMP_TABLE;
[MYSQL SSMS]
select emp_name,
CASE
WHEN CHARINDEX(' ', SUBSTRING(emp_name, CHARINDEX(', ',emp_name)+2)) >0
THEN SUBSTRING(SUBSTRING(SUBSTRING(emp_name, CHARINDEX(', ',emp_name)+2), ' '),1,CHARINDEX(' ',SUBSTRING(emp_name, CHARINDEX(', ',emp_name)+2)))
ELSE SUBSTRING(SUBSTRING(emp_name, CHARINDEX(', ',emp_name)+2), ' ')
END AS firstname,
SUBSTRING(emp_name, 1, CHARINDEX(',',emp_name) - 1) AS lastname
from EMP_TABLE;
I couldn't try it with MYSQL SSMS but the function CHARINDEX is the same as INSTR, the only difference is in INSTR you specify first where to look and then what to look.
I try it like this with your data and it worked.
Then, I convert every INSTR into CHARINDEX, and I inverted the parameters.
[DEMO]
with tmp as
(
select 'JONES, JAMES R' as emp_name from dual
union all
select 'SMITH, BILL' as emp_name from dual
)
select emp_name,
CASE
WHEN INSTR(SUBSTRING(emp_name, INSTR(emp_name,', ')+2),' ') >0
THEN SUBSTRING(SUBSTRING(SUBSTRING(emp_name, INSTR(emp_name,', ')+2), ' '),1,INSTR(SUBSTRING(emp_name, INSTR(emp_name,', ')+2),' '))
ELSE
SUBSTRING(SUBSTRING(emp_name, INSTR(emp_name,', ')+2), ' ')
END AS firstname,
SUBSTRING(emp_name, 1, INSTR(emp_name,',') - 1) AS lastname
from tmp;

Split FullName Where has null values (db2 sql)

I have full name that contains blank or null value and cause an error when I split into first name,lastname.
here is the error:
ERROR [22011] [IBM][DB2/AIX64] The statement was not executed because a numeric argument of a scalar function is out of range.
here is my original code:
UPPER(right(AGENT_NM, (char_length (AGENT_NM) - position( ' ', AGENT_NM))))|| ', ' || UPPER(left(AGENT_NM, position( ' ', AGENT_NM) - 1)) AS AGENT_NAME,
here is what I have tried:
1-
CASE when REGEXP_COUNT(AGENT_NM,',')> 0 then left (AGENT_NM, position( ' ', AGENT_NM) - 1) END AS FNAME,
2-
CASE when(AGENT_NM= ' ') then Null Else left (AGENT_NM, position( '
> ', AGENT_NM) - 1) END AS FNAME,
However it returns blank.
Concatenate a space onto the end of your name first:
SELECT
TRIM(UPPER(RIGHT(AGENT_NMs, char_length (AGENT_NMs) - position( ' ', AGENT_NMs))))||
', ' ||
TRIM(UPPER(left(AGENT_NMs, position( ' ', AGENT_NMs) - 1)))
AS AGENT_NAME
FROM
(
SELECT
a.*,
COALESCE(agent_nm, '')||' ' as agent_nms
FROM
yourtable a
) x
Here we use coalesce to ensure the name is not null, and we then add a space on as something for the position function to find
The top bit is just your code tweaked to refer to the new name agent_nms and add an extra trim command for removing any extraneous spaces. It did look like it had one too many brackets in by the way
name=str(input("your name here"))
split_name=name.split(" ")

Separate fullname into first and last, and remove 'junk'

Wasn't sure of the best way to word this. So I have a column with names, as below:
SalesPerson_Name
----------------
Undefined - 0
Sam Brett-sbrett
Kelly Roberts-kroberts
Michael Paramore-mparamore
Alivia Lawler-alawler
Ryan Hooker-rhooker
Heather Alford-halford
Cassandra Blegen-cblegen
JD Holland-jholland
Vendor Accounts-VENDOR
Other Accounts-OTHER
Getting the names separated is easy enough with PARSENAME and REPLACE functions, but where I'm running into a pickle is with getting rid of the 'junk' at the end:
SELECT SalesPerson_Key
,SalesPerson_Name
,CASE
WHEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2) IS NULL
THEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1)
ELSE PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2)
END AS FirstName
,CASE
WHEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2) IS NULL
THEN NULL
ELSE PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1)
END AS LastName
FROM Salesperson
RESULTS FOR LASTNAME COLUMN:
LastName
--------
0
Brett-sbrett
Roberts-kroberts
Paramore-mparamore
Lawler-alawler
Hooker-rhooker
Alford-halford
Blegen-cblegen
Holland-jholland
Accounts-VENDOR
Accounts-OTHER
Specifically, I want to get rid of the text (userid) at the end of the last name. If the names were the same length, I could just use a RIGHT function, but they vary in length. Ideas?
select left(PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1), len(SalesPerson_Name)-CHARINDEX('-',SalesPerson_Name)-1)
You are getting charindex of - and taking the left string of it.
If you just want to remove the last word (username) you can use a query like this
select
rtrim(
substring(
SalesPerson_Name,
1,
charindex('-',SalesPerson_Name,1)-1
)
)
from Salesperson
The charindex function locates the occurrence of the character/s you are looking for.
Consider whether hyphen is followed by a space or not, and split depending on these two cases
with Salesperson( SalesPerson_Name ) as
(
select 'Undefined - 0' union all
select 'Sam Brett-sbrett' union all
select 'Kelly Roberts-kroberts' union all
select 'Michael Paramore-mparamore' union all
select 'Alivia Lawler-alawler'
)
select case when substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+1,1) = '-' then
substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+3,len(SalesPerson_Name))
else
substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+1,len(SalesPerson_Name))
end as last_name
from Salesperson s;
last_name
------------------
0
Brett-sbrett
Roberts-kroberts
Paramore-mparamore
Lawler-alawler

Parsing Name Field in SQL

I am trying to separate a name field into the appropriate fields. The name field is not consistently the same. It can show up as Doe III,John w or Doe,John, or Doe III,John, or Doe,John W or it may be lacking the suffix and or middle initial. Any ideas would be greatly appreciated.
SELECT (
CASE LEN(REPLACE(FirstName, ' ', ''))
WHEN LEN(FirstName + ' ') - 1
THEN PARSENAME(REPLACE(FirstName, ' ', '.'), 2)
ELSE PARSENAME(REPLACE(FirstName, ' ', '.'), 3)
END
) AS LastName
,(
CASE LEN(REPLACE(FirstName, ' ', ''))
WHEN LEN(FirstName + ',') - 1
THEN NULL
ELSE PARSENAME(REPLACE(FirstName, ' ', '.'), 2)
END
) AS Suffix
,PARSENAME(REPLACE(FirstName, ' ', '.'), 1) AS FirstName
FROM Trusts.dbo.tblMember
I need the name regardless of the format, as stated above, to parse into the appropriate fields of LastName,Suffix,FirstName,MiddleInitial, regardless of whether it has a suffix or a middle initial
If the given 4 names are the only type of cases, then you can use something like below.
Note: I used a CTE table tbl2 to separate comma_pos,first_space,second_space for better understanding in the main query. You can replace these value in main query with their corresponding function in CTE, to make the main query faster. I mean replace comma_pos in main query with charindex(',',name) an so on.
Also I am assuming that there are no leading/trailing or extra whitespaces or any junk character in name column. If you have, then sanitize your data first before proceeding.
Rexter Sample
with tbl2 as (
select tbl.*,
charindex(',',name) as comma_pos,
charindex(' ',name,1) first_space,
charindex(' ',name,charindex(' ',name,1)+1) second_space
from tbl)
select tbl2.name
,case when second_space <> 0
then substring(name,comma_pos+1,second_space-comma_pos-1)
when first_space > comma_pos
then substring(name,comma_pos+1,first_space-comma_pos-1)
else substring(name,comma_pos+1,len(name)-comma_pos)
end as first_name
,case when second_space <> 0
then substring(name,second_space+1,len(name)-second_space)
when first_space > comma_pos
then substring(name,first_space+1,len(name)-first_space)
end as middle_name
,case when first_space=0 or first_space>comma_pos
then substring(name,1,comma_pos-1)
else substring(name,1,first_space-1)
end as last_name
,case when first_space=0 or first_space>comma_pos
then null
else substring(name,first_space,comma_pos-first_space)
end as suffix
from tbl2;

Concatenate and format text in SQL

I need to concatenate the City, State and Country columns into something like City, State, Country.
This is my code:
Select City + ', ' + State + ', ' + Country as outputText from Places
However, because City and State allow null (or empty) value, what happen is, (for example) if the City is null/empty, my output will look like , Iowa, USA; or say the State is empty, then the output will look like Seattle, , USA
Is there anyway I can format the output and remove "unnecessary" commas?
Edited: Because of the requirements, I should not use any other mean (such as PL/SQL, Store Procedure) etc., so it has to be plain SQL statement
select
isnull(City, '') +
case when isnull(City, '') != '' then ', ' else '' end +
isnull(State, '') +
case when isnull(State, '') != '' then ', ' else '' end +
isnull(Country, '') as outputText
from
Places
Since adding a string with null will result null so if they are null (not empty string) this will give you teh desired result
Select isnull(City + ', ','') + isnull(State + ', ' ,'') + isnull(Country,'') as outputText from Places
Use the COALESCE (Transact-SQL) function.
SELECT COALESCE(City + ', ', '') + COALESCE(State + ', ', '')...
In SQL Server 2012 you can use CONCAT function:
select concat(City,', ',State,', ',Country ) as outputText from Places
Not elegant by any means...
first changes city, state,country to null values if blank
then interprets that value for null and adds a space before a comma
then replaces any space comma space ( , ) with empty set.
Query:
SELECT replace(coalesce(Replace(City,'',Null),' ') + ', ' +
coalesce(Replace(State,'',Null), ' ' + ', ' +
coalesce(replace(Country,''Null),''), ' , ','') as outputText
FROM Places
Assumes no city state or country will contain space comma space.