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

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

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 concatenate columns for search

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,'%')

Does Space Take Up a Character Count in SQL Server

I have 3 columns:
Name nvarchar 100
FirstName nvarchar 50
LastName nvarchar 50
Can Name take in FirstName + ' ' + LastName if both are maxed out (50 characters)?
Can Name take in FirstName + ' ' + LastName if both are maxed out (50 characters)?
No.
If FirstName and LastName actually use the entire 50 char column width, there's no room for the space, and the new string containing the space won't fit.
OTOH, there's really no reason to store first and last then another column containing them both glued together. This could easily be done in the SQL query as needed with SELECT Firstname + ' ' + LastName as [Name] FROM TableName
In fact, it's better to not store first + space + last because once you get two copies of any chunk of data, eventually one is going to be wrong. Someone will have a name change and the "Name" field won't get updated.

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.

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'