仅当两列都具有值时才合并两列

combine two columns only if both column has value in it

我正在尝试过滤数据, 所以我有两列,都可以有一些值或空白(没有 space) 我只想在两个列都具有值时检索数据。

select CustFName + ' ' + JointCustFName as [BothName]
from table

我得到了所有的结果,但我想避免空值或空值。

例如

CustFName
'John'
'Bishop'
'Mark'

JointCustFName
'Mary'
''(blank)
'Wendy'

在这种情况下我只想看看 约翰玛丽 马克·温迪

因为没有与 CustFName 相关的 JointCustFName

只需添加一个带有 IS NOT NULL 的 where 子句来过滤空值和 <> ''

SELECT CustFName + ' ' + JointCustFName as [BothName]
FROM table
WHERE custFName IS NOT NULL and JointCustFName IS NOT NULL
      and custFNAME <> '' and JointCustFName <> ''