{"id":947,"date":"2016-07-16T14:09:20","date_gmt":"2016-07-16T14:09:20","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=947"},"modified":"2025-02-08T06:16:07","modified_gmt":"2025-02-08T13:16:07","slug":"sql-nullif","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-nullif\/","title":{"rendered":"SQL NULLIF Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL <code>NULLIF<\/code> function to compare two values and return <code>NULL<\/code> if they are equal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-sql-nullif-function'>Introduction to the SQL NULLIF function <a href=\"#introduction-to-the-sql-nullif-function\" class=\"anchor\" id=\"introduction-to-the-sql-nullif-function\" title=\"Anchor for Introduction to the SQL NULLIF function\">#<\/a><\/h2>\n\n\n\n<p>The <code>NULLIF<\/code> function compares two values and returns <code>NULL<\/code> if they are equal. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>NULLIF<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">NULLIF(value1,value2);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>NULLIF<\/code> function returns <code>NULL<\/code> if <code>value1<\/code> equals <code>value2<\/code>. It returns <code>value1<\/code> if the values are not equal.<\/p>\n\n\n\n<p>The <code>NULLIF<\/code> function is equivalent to the following searched <a href=\"https:\/\/www.sqltutorial.org\/sql-case\/\"><code>CASE<\/code><\/a> expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CASE\n  WHEN value1 = value1 THEN NULL\n  ELSE value1\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='basic-sql-nullif-function-examples'>Basic SQL NULLIF function examples <a href=\"#basic-sql-nullif-function-examples\" class=\"anchor\" id=\"basic-sql-nullif-function-examples\" title=\"Anchor for Basic SQL NULLIF function examples\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>NULLIF<\/code> function with two arguments are 100:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">NULLIF<\/span>(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">result<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIE5VTExJRigxMDAsIDEwMCkgcmVzdWx0Ow%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> result\n<span class=\"hljs-comment\">--------<\/span>\n   NULL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example returns the first argument which is <code>100<\/code> because <code>100<\/code> is not equal to <code>200<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">NULLIF<\/span>(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">200<\/span>) <span class=\"hljs-keyword\">result<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIE5VTExJRigxMDAsIDIwMCkgcmVzdWx0Ow%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> result\n<span class=\"hljs-comment\">--------<\/span>\n    100<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement returns the first argument which is <code>100<\/code> because <code>100<\/code> is not equal to <code>NULL<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">NULLIF<\/span>(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-literal\">NULL<\/span>) <span class=\"hljs-keyword\">result<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIE5VTExJRigxMDAsIE5VTEwpIHJlc3VsdDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> result\n<span class=\"hljs-comment\">--------<\/span>\n    100<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement returns the first argument which is <code>NULL<\/code> because <code>NULL<\/code> is not equal to <code>100<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">NULLIF<\/span>(<span class=\"hljs-literal\">NULL<\/span>, <span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">result<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIChOVUxMLCAxMDApIHJlc3VsdDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> result\n<span class=\"hljs-comment\">--------<\/span>\n   NULL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement returns the <code>NULL<\/code> because the string <code>SQL&nbsp;<\/code>is equal to the string&nbsp;<code>SQL<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">NULLIF<\/span>(<span class=\"hljs-string\">'SQL'<\/span>, <span class=\"hljs-string\">'SQL'<\/span>) <span class=\"hljs-keyword\">result<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIE5VTExJRignU1FMJywgJ1NRTCcpIHJlc3VsdDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> result\n<span class=\"hljs-comment\">--------<\/span>\n NULL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-nullif-function-with-table-data'>Using NULLIF function with table data <a href=\"#using-nullif-function-with-table-data\" class=\"anchor\" id=\"using-nullif-function-with-table-data\" title=\"Anchor for Using NULLIF function with table data\">#<\/a><\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqltutorial.org\/sql-create-table\/\">create a new table<\/a> called<code>articles<\/code> to store articles:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> articles (\n  article_id <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n  title <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  excerpt <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>),\n  <span class=\"hljs-keyword\">body<\/span> <span class=\"hljs-built_in\">TEXT<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRBQkxFIGFydGljbGVzICggYXJ0aWNsZV9pZCBJTlQgUFJJTUFSWSBLRVksIHRpdGxlIFZBUkNIQVIoMjU1KSBOT1QgTlVMTCwgZXhjZXJwdCBWQVJDSEFSKDI1NSksIGJvZHkgVEVYVCApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Second, <a href=\"https:\/\/www.sqltutorial.org\/sql-insert\/\">insert some rows<\/a> into the <code>articles<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  articles (article_id, title, excerpt, <span class=\"hljs-keyword\">body<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (\n    <span class=\"hljs-number\">1<\/span>,\n    <span class=\"hljs-string\">'SQL NULLIF function'<\/span>,\n    <span class=\"hljs-string\">''<\/span>,\n    <span class=\"hljs-string\">'This tutorial shows you how to use the SQL NULLIF function'<\/span>\n  ),\n  (\n    <span class=\"hljs-number\">2<\/span>,\n    <span class=\"hljs-string\">'SQL tutorial'<\/span>,\n    <span class=\"hljs-string\">'Learn how to use SQL at sqltutorial.org'<\/span>,\n    <span class=\"hljs-string\">'You will learn SQL with practical examples'<\/span>\n  ),\n  (\n    <span class=\"hljs-number\">3<\/span>,\n    <span class=\"hljs-string\">'SQL query'<\/span>,\n    <span class=\"hljs-literal\">NULL<\/span>,\n    <span class=\"hljs-string\">'You will learn how to use SELECT statement to query data from tables'<\/span>\n  );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=SU5TRVJUIElOVE8KICBhcnRpY2xlcyAoYXJ0aWNsZV9pZCwgdGl0bGUsIGV4Y2VycHQsIGJvZHkpClZBTFVFUwogICgKICAgIDEsCiAgICAnU1FMIE5VTExJRiBmdW5jdGlvbicsCiAgICAnJywKICAgICdUaGlzIHR1dG9yaWFsIHNob3dzIHlvdSBob3cgdG8gdXNlIHRoZSBTUUwgTlVMTElGIGZ1bmN0aW9uJwogICksCiAgKAogICAgMiwKICAgICdTUUwgdHV0b3JpYWwnLAogICAgJ0xlYXJuIGhvdyB0byB1c2UgU1FMIGF0IHNxbHR1dG9yaWFsLm9yZycsCiAgICAnWW91IHdpbGwgbGVhcm4gU1FMIHdpdGggcHJhY3RpY2FsIGV4YW1wbGVzJwogICksCiAgKAogICAgMywKICAgICdTUUwgcXVlcnknLAogICAgTlVMTCwKICAgICdZb3Ugd2lsbCBsZWFybiBob3cgdG8gdXNlIFNFTEVDVCBzdGF0ZW1lbnQgdG8gcXVlcnkgZGF0YSBmcm9tIHRhYmxlcycKICApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Third,  retrieve <code>article_id<\/code>, <code>title<\/code> and <code>excerpt<\/code> from the <code>articles<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  article_id,\n  title,\n  excerpt\n<span class=\"hljs-keyword\">FROM<\/span>\n  articles;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGFydGljbGVfaWQsIHRpdGxlLCBleGNlcnB0IEZST00gYXJ0aWNsZXM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> article_id |        title        |                 excerpt\n<span class=\"hljs-comment\">------------+---------------------+-----------------------------------------<\/span>\n          1 | SQL NULLIF function |\n          2 | SQL tutorial        | Learn how to <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">at<\/span> sqltutorial.org\n          <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">query<\/span>           | <span class=\"hljs-literal\">NULL<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the <code>excerpt<\/code> is not available, we can&nbsp;use the first 50 characters of the <code>body<\/code> instead. <\/p>\n\n\n\n<p>To do that, we use the <a href=\"https:\/\/www.sqltutorial.org\/sql-functions\/sql-coalesce\/\"><code>COALESCE<\/code><\/a> function to return <code>excerpt<\/code> column if it is not <code>NULL<\/code>, otherwise, return the first 50 characters of the <code>body<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  article_id,\n  title,\n  <span class=\"hljs-keyword\">COALESCE<\/span>(excerpt, <span class=\"hljs-keyword\">LEFT<\/span>(<span class=\"hljs-keyword\">body<\/span>, <span class=\"hljs-number\">50<\/span>)) <span class=\"hljs-keyword\">AS<\/span> summary\n<span class=\"hljs-keyword\">FROM<\/span>\n  articles;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGFydGljbGVfaWQsIHRpdGxlLCBDT0FMRVNDRShleGNlcnB0LCBMRUZUKGJvZHksIDUwKSkgQVMgc3VtbWFyeSBGUk9NIGFydGljbGVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> article_id |        title        |                      summary\n<span class=\"hljs-comment\">------------+---------------------+----------------------------------------------------<\/span>\n          1 | SQL NULLIF function |\n          2 | SQL tutorial        | Learn how to <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">at<\/span> sqltutorial.org\n          <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">query<\/span>           | You will learn how <span class=\"hljs-keyword\">to<\/span> <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">statement<\/span> <span class=\"hljs-keyword\">to<\/span> quer<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, The <code>summary<\/code> column is empty for the article id 1. To solve this problem, as you may guess, we use the <code>NULLIF<\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    article_id, \n    title, \n    <span class=\"hljs-keyword\">COALESCE<\/span>(<span class=\"hljs-keyword\">NULLIF<\/span>(excerpt,<span class=\"hljs-string\">''<\/span>), <span class=\"hljs-keyword\">LEFT<\/span>(<span class=\"hljs-keyword\">body<\/span>, <span class=\"hljs-number\">50<\/span>)) <span class=\"hljs-keyword\">AS<\/span> summary\n<span class=\"hljs-keyword\">FROM<\/span>\n    articles;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGFydGljbGVfaWQsIHRpdGxlLCBDT0FMRVNDRShOVUxMSUYoZXhjZXJwdCwgJycpLCBMRUZUKGJvZHksIDUwKSkgQVMgc3VtbWFyeSBGUk9NIGFydGljbGVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"> article_id |        title        |                      summary\n<span class=\"hljs-comment\">------------+---------------------+----------------------------------------------------<\/span>\n          1 | SQL NULLIF function | This tutorial shows you how to <span class=\"hljs-keyword\">use<\/span> the <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">NULLIF<\/span>\n          <span class=\"hljs-number\">2<\/span> | <span class=\"hljs-keyword\">SQL<\/span> tutorial        | Learn how <span class=\"hljs-keyword\">to<\/span> <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">at<\/span> sqltutorial.org\n          <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">query<\/span>           | You will learn how <span class=\"hljs-keyword\">to<\/span> <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">statement<\/span> <span class=\"hljs-keyword\">to<\/span> quer<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the <code>excerpt<\/code> is empty, the <code>NULLIF<\/code> function returns <code>NULL<\/code>, otherwise it returns the excerpt. Then the rest is handled by the <code>COALESCE<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>NULLIF<\/code> function to compare two values and returns <code>NULL<\/code> if they are equal. <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=nullif\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n\n\n\n<h2 class=\"wp-block-heading\" id='databases'>Databases <a href=\"#databases\" class=\"anchor\" id=\"databases\" title=\"Anchor for Databases\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-nullif\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL NULLIF Function<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-nullif\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL NULLIF Function<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-functions\/sqlite-nullif\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite NULLIF Function<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.oracletutorial.com\/oracle-comparison-functions\/oracle-nullif\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle NULLIF Function<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-nullif\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL Server NULLIF Function<\/a><\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"947\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-nullif\/\"\n\t\t\t\tdata-post-title=\"SQL NULLIF Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"947\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-nullif\/\"\n\t\t\t\tdata-post-title=\"SQL NULLIF Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal. Introduction to the SQL NULLIF function # The NULLIF function compares two values and returns NULL if they are equal. Here&#8217;s the syntax of the NULLIF function: The NULLIF function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":51,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-947","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL NULLIF Function<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqltutorial.org\/sql-nullif\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL NULLIF Function\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-nullif\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-08T13:16:07+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-nullif\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-nullif\/\",\"name\":\"SQL NULLIF Function\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"datePublished\":\"2016-07-16T14:09:20+00:00\",\"dateModified\":\"2025-02-08T13:16:07+00:00\",\"description\":\"In this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-nullif\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-nullif\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-nullif\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL NULLIF Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL NULLIF Function","description":"In this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqltutorial.org\/sql-nullif\/","og_locale":"en_US","og_type":"article","og_title":"SQL NULLIF Function","og_description":"In this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal.","og_url":"https:\/\/www.sqltutorial.org\/sql-nullif\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-08T13:16:07+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-nullif\/","url":"https:\/\/www.sqltutorial.org\/sql-nullif\/","name":"SQL NULLIF Function","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"datePublished":"2016-07-16T14:09:20+00:00","dateModified":"2025-02-08T13:16:07+00:00","description":"In this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-nullif\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-nullif\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-nullif\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL NULLIF Function"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=947"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/947\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}