joining two columns, display in merged columns becomes null - sql

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'

Related

Using ISNULL does not produce the expected results

I am trying to concatenate several columns of a person's name into one column and ultimately into a temporary table. I am unable to get the ISNULL function to work correctly.
I have tried using ISNULL to effectively say "if this column is blank, then just ignore it". I've read about a command called ISBLANK but that doesn't seem to work on my version of SQL Server.
SELECT
co.serialnumber, co.envelopesalutation,
co.title + ' ' + LEFT(co.firstname, 1) + ' ' + ISNULL(LEFT(co.otherinitial, 1), '') +
' ' + co.keyname + ' ' + ISNULL(co.POSTNOMINAL, '') [Correct]
INTO
TEMPENVSALUTATION
FROM
contact co
WHERE
co.contacttype = 'Individual'
AND co.title IN ('Mr', 'Mrs', 'Ms', 'Miss', 'Mx', 'Dr')
I expect, for example, that someone with co.title of Mr, a co.firstname of Jon, no co.otherinitial a co.keyname of Smith and no co.postnominal to appear in the TEMPENVSALUTATION table as Mr J Smith
What I'm actually getting is only people who have a co.otherinitial appearing e.g. Mr S D Smith.

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

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

How to glue 3 rows in one string?

In table pics I have 3 different rows, where surname, name and middle name are stored.
In query I need to glue them to one string:
pics.e_family + ' ' + pics.e_name + ' ' + pics.e_sname AS fio
All works perfectly, but one entry don't have middle name at all (no mistake, its really so). And in fio I get NULL. Is there any possibility to modify code, or make additional check if one or more of the rows are null, replace it with space symbol or just show remain rows?
If you mean columns instead of rows, you can use COALESCE or ISNULL:
pics.e_family + ' ' + COALESCE(pics.e_name, '') + ' ' + pics.e_sname AS fio

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