Split and merge string - sql

I'm trying to generate an email from full name. Following is what I have and I'm stuck here
select distinct tbin.Name
,(SELECT SUBSTRING(tbin.Name, 1, CHARINDEX(' ', tbin.Name) - 1) AS [FirstName])
,(select SUBSTRING(tbin.Name, CHARINDEX(' ', tbin.Name) + 1, 8000) AS LastName)
from tblInitialApplicants tbin
Name is like 'Natalie Marine Baily'. Name can be of any length and also can have any number of space between each part of the name.
Expected result: Natalie.B#gmail.com
'Natalie' - First Name
'B' - First letter of the last name

You can get the position of a char with CHARINDEX, look for the first space and reverse de name and look for the last. With that you can easly substring first and last name.
DECLARE #Fullname VARCHAR(100) = 'Natalie Marine Baily';
select
*,
firstname+'.'+lastnameINITIAL+'#gamil.com'
from (
select
#Fullname Fullname,
SUBSTRING(#Fullname,1,CHARINDEX(' ',#Fullname,1)) firstname,
SUBSTRING(SUBSTRING(#Fullname,LEN(#Fullname)-CHARINDEX(' ',REVERSE(#Fullname),1)+2,CHARINDEX(' ',REVERSE(#Fullname),1)),1,1) lastnameINITIAL
) T

DECLARE #name VARCHAR(200) = 'Natalie Marine Baily'
SELECT LEFT(#name,CHARINDEX(' ',#name,1)-1) + '.'+
LEFT(REVERSE(LEFT(REVERSE(#name),CHARINDEX(' ',REVERSE(#name),1)-1)),1) + '#gmail.com'
Result:
Just see, whether you can work from here.

Your biggest problem is that both first names and last names can have spaces between them. So how will you know where the last name begins? For example Jean-Claude Van Damme.
Also what should the email look like in this case? jean-claude.v#gmail.com?
I would suggests you change your database to store the first and last names as separate fields, or better yet have an email field. Then this will be much easier. If you cannot do that you will have to generate a few possible emails and validate if the emails exist.
How to check if an email address exists without sending an email?
This would be how to generate an email if you take it that the first word is the firstname and the last word is the lastname.
DECLARE #Name varchar(100) = 'Natalie Marine Baily'
select SUBSTRING(#Name, 0, CHARINDEX(' ', #Name) - 1)
+ '.' + SUBSTRING(#Name,LEN( #Name) - CHARINDEX(' ',REVERSE(#Name))+2 , 1) + '#gmail.com'

Related

SQL Server Parsing Thai Language Full Name to First Last

I have a unique issue. I've worked a long time with SQL Server. We import a file into SQL Server that includes Full Name. All I need to do is parse the full name into First and Last. If the name is English character set, my parse works fine. But, we're parsing Thai names which uses a different character set?
This is my code:
DECLARE #FullName NVARCHAR(MAX) = N'กล้วยไม้ สวามิวัศดุ์'
SELECT
LEN(#FullName) AS StringLength,
#FullName AS FullName,
REVERSE(#FullName) AS ReverseName,
LEFT(#FullName, LEN(#FullName) - CHARINDEX(' ', REVERSE(#FullName))) AS FirstName,
STUFF(RIGHT(#FullName, CHARINDEX(' ', REVERSE(#FullName))),1,1,'') AS LastName;
Results:
20 กล้วยไม้ สวามิวัศดุ์ ์ุดศัวิมาวส ้มไยว้ลก กล้วยไม้ สวามิวัศดุ์ NULL
Like I said, the query works fine when I use english. For example, 'John Smith' returns:
10 John Smith htimS nhoJ John Smith
I have searched this and other sites all day! Thanks in advance.
I've seen this (IMO) complicated logic involving reverse and stuff previously and do not understand the purpose of using reverse. It just seems overly complicated to me. Below does what you want without using reverse.
set nocount on;
declare #names table (FullName nvarchar(40) not null);
insert #names (FullName) values (N'กล้วยไม้ สวามิวัศดุ์'), (N'John Smith'), (N'Bozo Clown'), (N'Eva Perón'), (N'Stefan Löfven');
select * from #names;
select
LEN(FullName) AS StringLength,
FullName AS FullName,
LEFT(FullName, CHARINDEX(N' ', FullName)) AS FirstName,
RIGHT(FullName, len(FullName) - CHARINDEX(N' ', FullName) + 1) as LastName,
STUFF(FullName, 1, CHARINDEX(N' ', FullName), N'') as LName
from #names
;
For consistency (a habit you should develop) I've changed your string literals from ansi strings to unicode strings (prefaced with N'). And a word of warning - names are complicated. Verify your assumption about always having 2 part names.

SQL Server : separate first and last name and remove middle initial into just two columns

I have a column that has an individuals full name, including the middle initial. I am trying to separate the full name into just first name and last name and eliminating the middle name/initial. Some of the names in my database have a middle name/initial and some don't. The following query's are what I am using and they both do only half the trick.
Query #1: returns the first name and middle name/initial in the 2nd column and eliminates the last name:
FirstName = LEFT(fullname, CHARINDEX(' ', fullname)),
LastName = RIGHT(fullname, CHARINDEX(' ', REVERSE(fullname)))
Query #2: returns the first name and combines the middle name/initial with the last name (with a space between the two):
FirstName = left(fullname,CHARINDEX(' ',fullname)),
LastName = SUBSTRING(fullname, CHARINDEX(' ',fullname)+1,LEN(fullname)-(CHARINDEX(' ',fullname)-1))
How about this?
select left(fullname, charindex(' ', fullname + ' ') - 1) as firstname,
right(fullname, charindex(' ', reverse(fullname) + ' ') - 1) as lastname
Note this handles names with no spaces without giving an error. However, the first and last name are the same (the full string). It is unclear what you want to do in this case.

Using SQL Server to replace line breaks in columns with spaces

I have a large table of data where some of my columns contain line breaks. I would like to remove them and replace them with some spaces instead.
Can anybody tell me how to do this in SQL Server?
Thanks in advance
SELECT REPLACE(REPLACE(#str, CHAR(13), ''), CHAR(10), '')
This should work, depending on how the line breaks are encoded:
update t
set col = replace(col, '
', ' ')
where col like '%
%';
That is, in SQL Server, a string can contain a new line character.
#Gordon's answer should work, but in case you're not sure how your line breaks are encoded, you can use the ascii function to return the character value. For example:
declare #entry varchar(50) =
'Before break
after break'
declare #max int = len(#entry)
; with CTE as (
select 1 as id
, substring(#entry, 1, 1) as chrctr
, ascii(substring(#entry, 1, 1)) as code
union all
select id + 1
, substring(#entry, ID + 1, 1)
, ascii(substring(#entry, ID + 1, 1))
from CTE
where ID <= #max)
select chrctr, code from cte
print replace(replace(#entry, char(13) , ' '), char(10) , ' ')
Depending where your text is coming from, there are different encodings for a line break. In my test string I put the most common.
First I replace all CHAR(10) (Line feed) with CHAR(13) (Carriage return), then all doubled CRs to one CR and finally all CRs to the wanted replace (you want a blank, I put a dot for better visability:
Attention: Switch the output to "text", otherwise you wont see any linebreaks...
DECLARE #text VARCHAR(100)='test single 10' + CHAR(10) + 'test 13 and 10' + CHAR(13) + CHAR(10) + 'test single 13' + CHAR(13) + 'end of test';
SELECT #text
DECLARE #ReplChar CHAR='.';
SELECT REPLACE(REPLACE(REPLACE(#text,CHAR(10),CHAR(13)),CHAR(13)+CHAR(13),CHAR(13)),CHAR(13),#ReplChar);
I have the same issue, means I have a column having values with line breaks in it. I use the query
update `your_table_name` set your_column_name = REPLACE(your_column_name,'\n','')
And this resolves my issue :)
Basically '\n' is the character for Enter key or line break and in this query, I have replaced it with no space (which I want)
Keep Learning :)
zain

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.

How do I trim out middle initial from a person's full name in SQL?

I'm look for the simplest way to split up first and last name wile trimming out the middle initial. The current layout of the field is [Last Name], [First Name] [MI]. Also, middle initial is not always there. My current code is below, I'm just not sure how to trim out the middle initial from first name without writing a case statement.
SELECT SUBSTRING(h.Name, CHARINDEX(',', h.Name, 0) + 2, LEN(h.Name) - CHARINDEX(',', h.Name, 0)), 0 as FirstName
,SUBSTRING(h.Name, 0, CHARINDEX(',', h.Name, 0)) as LastName
FROM Members
I have made some assumptions below:
1 - First names are always longer than one character.
2 - Middle inital will always be preceded by a space.
3 - The data is trimmed.
This code will return NULL if any of the above are not true. If your data is not trimmed, you can use RTRIM on all instances of #n below to mitigate.
declare #n as varchar(50)
set #n = 'Smith, John A'
select #n,
case
when SUBSTRING(#n, LEN(#n) - 1, 1) = ' '
then SUBSTRING(#n, LEN(#n), 1)
end
What are the business rules of this system? Will it always be:
last name , first name space a possible middle initial
What other permutations can exist?
Will it always be space letter . ? Because then you could always take the right three characters, look for a space and a period, then remove the set of three.
select REPLACE(firstName+ISNULL(middleName+' ','')+ISNULL( lastName +' ',''),' ',' ') as 'name' from Contacts