如果一行不存在,如何显示注释? SQL Server

时间:2016-11-17 14:43:50

标签: sql sql-server

我想显示customer(Customertable)的所有电话号码。

但如果由于客户没有留下电话号码而没有电话号码,我想显示一条说“没有电话号码”的说明。

我用命令

尝试过
Select phonenumber 
From Customers
if phonenumber is NUll RAISEERROR 'No phonenumber'  -error

但是我收到了一个错误:

  

“Phonenumber”不是有效的列名

我想RAISEERROR也不是显示音符的正确方法(?!)

请帮忙

抱歉英语不好。

4 个答案:

答案 0 :(得分:6)

为什么不使用coalesce返回第一个非空值?

SELECT
    COALESCE(phonenumber, 'no phone number')
FROM customers

答案 1 :(得分:1)

var Addresses = JSON.parse('{"With Address": 45532627,"Without Address": 10845793}'); var Age = JSON.parse('{"18-25": 2162586,"26-35": 9995883,"36-45": 9486158,"46-55": 8729677,"56-65": 6913371,"65+": 10545270}'); var Cellphone = JSON.parse('{"With Cellphone": 21536203,"Without Cellphone": 34842217}'); google.charts.load("current", {packages:["corechart"]}); google.charts.setOnLoadCallback(drawChart(Addresses,'Addresses')); google.charts.setOnLoadCallback(drawChart(Age,'Age')); google.charts.setOnLoadCallback(drawChart(Cellphone,'Cellphone')); function drawChart(Chartdata,name) { var data = new google.visualization.DataTable(Chartdata); var options = { title: name, pieHole: 0.4, legend: 'left' }; var chart = new google.visualization.PieChart(document.getElementById(name)); chart.draw(data, options); } 似乎相当野蛮。

你可以这样做:

RAISEEROR

SELECT COALESCE(MAX(phonenumber), 'No phonenumber') FROM Customers WHERE CustomerId = @CustomerId; 的目的是为给定的客户返回一行。

但是,这种逻辑应该位于应用程序层,可能不会嵌入查询中。

答案 2 :(得分:0)

你可以试试这个,我不知道你的意思是什么没有phonenumber,它可以是null或空char,所以这是我的意思:

Select case when phonenumber is null or phonenumber ="" then
'No phonenumber' 
else phonenumber end
 FROM Customers

答案 3 :(得分:0)

您可以使用ISNULL功能

  

ISNULL(check_expression,replacement_value)

请在MSDN

上找到说明

我希望这会对你有所帮助

SELECT
   ISNULL(phonenumber, 'no phone number')
FROM customers