Obtain first name, last name with unknown number of space - sql

I'm new in VB.NET programming but I have to do a research bar in my program. Actually, the search bar is looking for first name or for last name (If first name or last name have space it's working) but if I want to search for both, how can I build my sql query? I don't know if the user will put first name first or last name first. What I've tried is to split the names if there is a space but if the person have a name like 'Robert Downey Junior', how can I know which part of the space will be the first name or the last name?
So,
how can I build my sql query for this task?
how can I know which part of the space will be the first name or the last name?
Actual regex
' value of query = "% + search bar + %" for sql LIKE parameter
Regex.IsMatch(query, "^'%(\w+ \w+)+%'$")
How I detect the first and last names
Dim name As String = query.ToString.Substring(2, query.Length - 4)
Dim nameParts As String() = name.Split(" ")
'So nameParts(0) = "robert", nameParts(1) = "downey" and namePart(2) = "junior"
Actual sql selector
SELECT id, firstName, lastName
FROM beneficiaire
WHERE LOWER(lastName) LIKE '%robert downey junior%'
OR LOWER(firstName) LIKE '%robert downey junior%'

CREATE PROCEDURE SEARCH( #SearchName VARCHAR(50)) AS
DECLARE #name VARCHAR(52)
SET '%' + #SearchName + '%'
SELECT id,
firstName,
lastName
FROM beneficiaire
WHERE (
LOWER(lastName) + ' ' + LOWER(firstName) LIKE #name
) OR
(
LOWER(firstName) + ' ' + LOWER(lastName) LIKE #name
)

Related

How to search name, (OPTIONAL) second name, surname on SQL?

I am not usually an SQL person but need to find matching name/surnames from a list of people on MSSql. I am trying to find similar names from TABLENAME to match the list of people that I have. The line looks like this:
Select *
from TABLENAME(nolock)
Where SomeRule='04' and RTRIM(Name) +' ' + RTRIM(SecondName) +' '+RTRIM(Surname) in (THE LIST OF PEOPLE HERE)
But this method only gives me the match of people with second names. If someone does not have a second name but their name+surname match, it does not show up. I want to see people who have 100% match of name-second name-surname OR name-surname (if they don't have second name).
Thank you guys in advance
Provided list of people is a table
Select distinct t.*
from TABLENAME t
join [THE LIST OF PEOPLE HERE] lp on SomeRule='04'
and RTRIM(t.Name) = lp.Name
and RTRIM(t.Surname) = lp.Surname
and (t.SecondName is null and pl.SecondName is null or RTRIM(t.SecondName) = lp.SecondName)
Let's look at some rows:
Name
SecondName
Surname
Remark
Your concatenation
'John'
'Thomas'
'Smith'
Person with middle name
'John Thomas Smith'
'John'
''
'Smith'
Person that has no middle name
'John  Smith'
'John'
null
'Smith'
Person with an unknown middle name
null
The person without a middle name cannot be found because of two blanks separating first and last name in your created string. The person of whom we don't know the middle name cannot be found because the concatenated string is null.
You can use CASE WHEN to check this and react accordingly:
select *
from tablename
where somerule = '04'
and
rtrim(name) + ' ' +
case when rtrim(secondname) is null then ''
when rtrim(secondname) = '' then ''
else rtrim(secondname) + ' '
end +
rtrim(surname) in (the list of people here)

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.

SQL Select query to pick the field value which has more than one empty space?

In my LastName Column, I have either one name or two names. In some records, I have more than one empty space between the two names.
I will have to select the records which has more than one empty space in the field name.
declare #nam nvarchar(4000)
declare #nam1 nvarchar(4000)
set #nam = 'sam' + ' ' + 'Dev'
set #nam1 = 'ed' + ' ' + ' ' + 'Dev'
In the sample query, i expect the output value should be #nam1.
You can do this using LEN and REPLACE to replace the spaces from string and then get original length - replaced length and then check that in WHERE clause,
SELECT *
FROM
mytTable
WHERE
LEN(LastName)-LEN(REPLACE(LastName, ' ', '')) > 1

SQL Concatenation Query - Four As in Concatenated Name

I am trying to create a view in SQL. We will call this view "A7T8". The view will pull first and last names from the database, and concatenate the first and last names into a field called FullName. However, I only want to select the concatenated names that have a total of at least four As in their first and last names. (Rebecca Aaronson would not be displayed since she only has 3 As, but Harry Flanagan would be displayed.
Currently my code is:
CREATE VIEW A7T8 AS
SELECT FNAME || ' ' || LNAME AS FULLNAME
FROM A7
WHERE Upper(LNAME) LIKE '%A%A%A%A%' OR Upper(FNAME) LIKE '%A%A%A%A%'
ORDER BY LNAME, FNAME;
But this only pulls names that have four As in the first name or four As in the last name. I want it to pull names that have a total of at least four As in the concatenated full name. How do I do this?
You can simply write:
CREATE VIEW A7T8 AS
SELECT FNAME || ' ' || LNAME AS FULLNAME
FROM A7
WHERE UPPER(FNAME || ' ' || LNAME) LIKE '%A%A%A%A%'
ORDER BY LNAME, FNAME;
You can try the below approach
CREATE VIEW A7T8 AS
select FNAME + ' ' + LNAME AS FULLNAME
from [A7]
where len(FNAME + LNAME) - len(Replace(Upper(FNAME) + Upper(LNAME),'A','')) >= 4
ORDER BY LNAME, FNAME;
I think Concat do the job, you must use it in a query not with the designer
CREATE VIEW [dbo].[DEMO_VIEW]
AS
SELECT Concat('IND_', LIBELLE) as SECONDLIBELLE,
CODE as SECONDCODE
from dbo.DEMOTABLE
GO
Use a REGEX. This will match first last with 1-4 A's
CREATE VIEW A7T8 AS
SELECT FNAME || ' ' || LNAME AS FULLNAME
FROM A7
WHERE CONCAT(Upper(LNAME), " ", Upper(FNAME)) REGEX '[A]{1,4}'
ORDER BY LNAME, FNAME;

Split and merge string

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'