我想要检索的是联系电话。我有一个名为BusinessDetails的关系,字段为Name
,Description
,PhoneNum
,MobileNum
。联系电话的情况如下:
我正在尝试这段sql select语句,当有这两个号码时它是相当不错的。但是在2
和3
的情况下,有一个不必要的逗号与之关联。我的sql是
SELECT Business.PhoneNum+','+Business.MobileNum AS ContactNumber
FROM dbo.BusinessDetails AS Business
在第二和第三种情况下,有些人可以帮我删除逗号吗?
答案 0 :(得分:2)
使用CONCAT_WS()
,如下所示:
select CONCAT_WS(',', Business.PhoneNum, Business.MobileNum)
from dbo.BusinessDetails as Business
答案 1 :(得分:1)
您可以使用case
(适用于几乎所有DBMS的通用解决方案):
select case
when PhoneNum is null then -- MobileNum only
MobileNum
when MobileNum is null then -- PhoneNum only
PhoneNum
when (PhoneNum is not null) and (MobileNum is not null) then -- Both
MobileNum || ',' || PhoneNum
else -- Neither MobileNum no PhoneNum
'No Phone'
end as ContactNumber
from dbo.BusinessDetails as Business