SQL concatenate columns for search - sql

I was looking for advice on the best way to implement search functionality over several columns.
I have a SQL server with columns for:
title
firstname
middlenames
lastname
maidenname
If a user searches for "John Smith" I need to look through all the columns for those values. Obviously, none of those columns will contain both parts of "John" and "Smith". I previously added the columns together eg
select * from PEOPLE where (firstname + ' ' + middlenames + ' ' + lastname) = %#SEARCHVALUE%
However this prevents certain searches returning values because the wildcard does not match the search criteria.

If one of your fields can have null value you should do CONCAT() them as well.
SELECT * FROM PEOPLE WHERE CONCAT(firstname, ' ', middlenames, ' ', lastname) LIKE CONCAT('%',#SEARCHVALUE,'%')

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)

Concatenating Full Names in Microsoft SQL Server

Suppose I have a table with the following information
Title FirstName MiddleName LastName Suffix
======|============|=============|============|======
Mr. John Fitzgerald Kennedy NULL
NULL John NULL Doe III
I want to create a query to output the full name of the above information. For example, I want the output to look like
Mr. John Fitzgerald Kennedy
John Doe, III
I was thinking of using the concat() function, but I am unsure how to go about this to ensure I don't have extra spaces or hanging commas in the names.
This is a little tricky. SQL Server 2017+ supports concat_ws(). However, you still need a comma for the suffix. Forturnately, concat() ignores NULLs:
select concat(concat_ws(' ', title, firstname, middlename, lastname),
', ' + suffix
)
Earlier versions require a bit more work to avoid multiple spaces when there are NULL values:
select ltrim(concat(' ' + title,
' ' + firstname,
' ' + middlename,
' ' + lastname,
', ' + suffix
)
)
This uses the fact that + returns NULL when an argument is NULL. But, concat() ignores it.
Note: Neither probably does what you want if all the names are NULL but the suffix is present. However, I doubt that is an issue in your data.
Here is a db<>fiddle.

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.

joining two columns, display in merged columns becomes null

I have a query that I am going to use in one of my stored procs. Specs dictate that, in the table, there should are 3 parts of an address, but there are going to be merged into one column: AddressLine1 which is the Street, AddressLine2 which is the Subdivision/, and AddressLine3 which is the City/State/Province. out of all these 3 fields, only AddressLine1 is mandatory, the other 2 can be left NULL. here is my code joining the 3 address fields into one:
(tb_TransactionName.AddressLine1 + ' ' + tb_TransactionName.AddressLine2 + ' ' + tb_TransactionName.AddressLine3) as 'Address'
Given that, I have entered text in the AddressLine1. but whenever I run the query, the Address column displays NULL. The moment i don't add up the 3 address fields and display only AddressLine1, the text under the AddressLine1 column appears in the results. For other info, the table tb_Transaction is left-joined to another table tb_TransactionDetails:
FROM (tb_TransactionType inner join tb_TransactionDetails
on tb_TransactionType.TxnTypeCode = tb_TransactionDetails.TxnType)
LEFT JOIN tb_TransactionName on tb_TransactionDetails.TxnID = tb_TransactionName.TxnID
any idea what could be the possible problem as to why the merged column displays NULL?
I bet one of the columns you are joining are with NULL value, which makes the result NULL.
try this:
(tb_TransactionName.AddressLine1 + ' ' + ISNULL(tb_TransactionName.AddressLine2, '') + ' ' + ISNULL(tb_TransactionName.AddressLine3, '')) as 'Address'
(ISNULL(tb_TransactionName.AddressLine1,'') + ' ' +
ISNULL(tb_TransactionName.AddressLine2,'') + ' ' +
ISNULL(tb_TransactionName.AddressLine3,'') as 'Address'

MS SQL: Retrieving "Varchar" data from 3 columns and combine them together

I have three columns in my sql table, "FirstName", "MiddleName","LastName". When retrieving, I need to display these 3 together, for an example
FirstName = "John"
MiddleName = "Ned"
LastName = "Carter".
On retrieving, these should be displayed as "John Ned Carter".
I tried the following
select FirstName+MiddleName+LastName from PhoneData
There is a problem!!! There are number of Names which the middle name is NULL. There are number of names which the last name is NULL, and so on. This is not retrieving those!!! It simply retrieve names where all the fields are not null!!!! If at least one column is null for a particular name, then it shows the whole name as NULL!!! For an example,
FirstName = "John"
MiddleName = NULL
LastName = NULL
on retrieval, the out put is ' NULL ' , not "John"
Please help!
Try the ISNULL() function around each field. Then you can set a value for when the selected value is null.
Like this;
select ISNULL(FirstName, '') + ISNULL(MiddleName, '') + ISNULL(LastName, '') from PhoneData