创艺人生 發表於 2025-9-4 11:04:11

Sql Server数据库中isnull、iif、case when三种方式的用法和空值判断

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">前言</a></li><li><a href="#_label1">ISNULL用法</a></li><li><a href="#_label2">case when用法</a></li><li><a href="#_label3">iif</a></li><li><a href="#_label4">查询小技巧</a></li><ul class="second_class_ul"><li><a href="#_lab2_4_0">技巧一</a></li><li><a href="#_lab2_4_1">技巧二</a></li></ul></ul></div><p class="maodian"><a name="_label0"></a></p><h2>前言</h2>
<p>有时候数据库你接触多了,就会知道一些技巧,说实在有时候博主没有接触到,我可能还是会用普通方式去判断,这样就会存在漏数据的情况。<br />所以,平时去积累一些技巧还是非常有必要的。</p>
<p class="maodian"><a name="_label1"></a></p><h2>ISNULL用法</h2>
<p>在 SQL Server 中,<code>ISNULL</code> 函数只接受两个参数,不支持三个参数的情况。<br />博主刚开始是这么用的 <code>ISNULL(t11.ItemCode,&#39;Y&#39;,&#39;N&#39;)</code> ,这种写法是错误的。</p>
<p><code>ISNULL</code> 函数的正确语法是:</p>
<div class="jb51code"><pre class="brush:sql;">ISNULL(check_expression, replacement_value)</pre></div>
<p class="maodian"><a name="_label2"></a></p><h2>case when用法</h2>
<p>如果需要实现类似三个参数的功能(当字段为 NULL 时返回 &lsquo;Y&rsquo;,否则返回 &lsquo;N&rsquo;),可以使用 <code>CASE WHEN</code> 表达式:</p>
<div class="jb51code"><pre class="brush:sql;">CASE WHEN t11.ItemCode IS NULL THEN 'Y' ELSE 'N' END</pre></div>
<p class="maodian"><a name="_label3"></a></p><h2>iif</h2>
<p>或者使用更简洁的 <code>IIF</code> 函数(SQL Server 2012 及以上版本支持):</p>
<div class="jb51code"><pre class="brush:sql;">IIF(t11.ItemCode IS NULL, 'Y', 'N')
</pre></div>
<p class="maodian"><a name="_label4"></a></p><h2>查询小技巧</h2>
<p class="maodian"><a name="_lab2_4_0"></a></p><h3>技巧一</h3>
<p>使用 <code>CONCAT</code> 安全地拼接字符串</p>
<p><strong>问题:</strong> 传统的用加号 <code>+</code> 拼接字符串时,如果任何一个字段为 <code>NULL</code>,整个结果都会变成 <code>NULL</code>。</p>
<p><strong>旧方法(有风险):</strong></p>
<div class="jb51code"><pre class="brush:sql;">SELECT FirstName + ' ' + LastName AS FullName
FROM Employees;
-- 如果 FirstName 或 LastName 为 NULL,FullName 就会显示为 NULL</pre></div>
<p><strong>小技巧(使用 </strong><code>CONCAT</code><strong>):</strong></p>
<div class="jb51code"><pre class="brush:sql;">SELECT CONCAT(FirstName, ' ', LastName) AS FullName
FROM Employees;
</pre></div>
<p><strong>好处:</strong></p>
<ul><li><code>CONCAT</code> 函数会自动将 <code>NULL</code> 值视为空字符串 <code>&#39;&#39;</code> 来处理。</li><li>即使 <code>FirstName</code> 或 <code>LastName</code> 为 NULL,其他部分依然会正常拼接,最终结果不会是 NULL。</li><li>代码更简洁易读。</li></ul>
<p class="maodian"><a name="_lab2_4_1"></a></p><h3>技巧二</h3>
<p>使用 <code>EXISTS</code> 代替 <code>IN</code> 来检查存在性</p>
<p><strong>问题:</strong> 当使用 <code>IN</code> 子查询时,数据库需要先执行整个子查询,返回所有结果集,然后再进行主查询和子查询结果的匹配,如果子查询结果集很大,性能会较差。</p>
<p><strong>旧方法(可能低效):</strong></p>
<div class="jb51code"><pre class="brush:sql;">SELECT *
FROM Customers
WHERE CustomerID IN (
    SELECT DISTINCT CustomerID
    FROM Orders
    WHERE OrderDate &gt; '2023-01-01'
);
</pre></div>
<p><strong>小技巧(使用 </strong><code>EXISTS</code><strong>):</strong></p>
<div class="jb51code"><pre class="brush:sql;">SELECT *
FROM Customers c
WHERE EXISTS (
    SELECT 1
    FROM Orders o
    WHERE o.CustomerID = c.CustomerID
    AND o.OrderDate &gt; '2023-01-01'
);
</pre></div>
<p><strong>好处:</strong></p>
<ul><li><strong>性能更高</strong>:<code>EXISTS</code> 是一种关联子查询,一旦找到一条满足条件的记录就会立即返回 <code>True</code> 并停止搜索,避免了处理整个子查询结果集。</li><li><strong>语义更清晰</strong>:<code>EXISTS</code> 直接表达了&ldquo;是否存在&rdquo;的逻辑意图。</li><li>在子查询结果集很大时,性能提升尤为明显。</li></ul>
<blockquote><p>这两个技巧一个侧重于<strong>数据的可靠性和整洁性</strong>,另一个侧重于<strong>查询的性能优化</strong>,都是日常开发中非常实用的&ldquo;利器&rdquo;。</p></blockquote>
頁: [1]
查看完整版本: Sql Server数据库中isnull、iif、case when三种方式的用法和空值判断