为了简化问题:
我有一个可以返回NULL的选择.
例如:
-- if the customer has no fax then NULL will be returned
select customerFax from customers
Run Code Online (Sandbox Code Playgroud)
我如何检查此null,然后返回一个字符串值而不是NULL,我需要在报告中显示一些友好的消息.
所以如果为null - 然后返回一些字符串,否则从db返回值...
我希望这很容易理解 - 谢谢
这通常使用内置的ISNULL函数完成:
SELECT ISNULL(faxNumber, 'No fax') FROM customers
Run Code Online (Sandbox Code Playgroud)
更新:感谢@gdn指出如果消息长度>字段长度,消息将被截断,在这种情况下,您可以通过将原始字段转换为与消息一样大来解决它
SELECT ISNULL(CAST(faxNumber AS VARCHAR(100), 'No fax specified at all, why didn''t you specify one before you asked me to display it for you? NEXT TIME MAYBE YOU SHOULD SPECIFY ONE.')
FROM customers
Run Code Online (Sandbox Code Playgroud)