<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SQL CONCAT returning null in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/sql-concat-returning-null/m-p/25594#M17831</link>
    <description>&lt;P&gt;Hi @Steve Helms​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There is drawback in SQL Concatenate function, as a workaround you can use COALESCE , COALESCE converts null values with empty string by wrapping in it .&lt;/P&gt;&lt;P&gt;Like this :&lt;/P&gt;&lt;P&gt;----&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT
substr(user.first, 0, 1),
user.first,
user.last,
CONCAT(COALESCE(substr(user.first,0,1),'')," ",COALESCE(`last`,'')) as abbr_name
FROM
user&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2018iDAD4F470502F9EE8/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Mar 2022 15:17:00 GMT</pubDate>
    <dc:creator>User16752245772</dc:creator>
    <dc:date>2022-03-15T15:17:00Z</dc:date>
    <item>
      <title>SQL CONCAT returning null</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-concat-returning-null/m-p/25593#M17830</link>
      <description>&lt;P&gt;Has anyone else experienced this problem? I'm attempting to SQL &lt;I&gt;concat&lt;/I&gt; two fields and if the second field is null, the entire string appears as null. The documentation is unclear on the expected outcome, and contrary to how &lt;I&gt;concat_ws&lt;/I&gt; operates.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT
  substr(users.first, 0, 1),
  users.first,
  users.last,
  CONCAT(
    substr(users.first, 0, 1),
    ' ',
    users.last
  ) as abbr_name
FROM
  users&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is an example of what I receive.&lt;span class="lia-inline-image-display-wrapper" image-alt="Screen Shot 2022-03-14 at 4.00.53 PM"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2035iEFEAA2B98C89001B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2022-03-14 at 4.00.53 PM" alt="Screen Shot 2022-03-14 at 4.00.53 PM" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have any suggestions on how to get the results I've shown &lt;B&gt;abbr_name&lt;/B&gt; without being entirely null if there is no last name?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Steve&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 21:05:30 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-concat-returning-null/m-p/25593#M17830</guid>
      <dc:creator>shelms</dc:creator>
      <dc:date>2022-03-14T21:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: SQL CONCAT returning null</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-concat-returning-null/m-p/25594#M17831</link>
      <description>&lt;P&gt;Hi @Steve Helms​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There is drawback in SQL Concatenate function, as a workaround you can use COALESCE , COALESCE converts null values with empty string by wrapping in it .&lt;/P&gt;&lt;P&gt;Like this :&lt;/P&gt;&lt;P&gt;----&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT
substr(user.first, 0, 1),
user.first,
user.last,
CONCAT(COALESCE(substr(user.first,0,1),'')," ",COALESCE(`last`,'')) as abbr_name
FROM
user&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2018iDAD4F470502F9EE8/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Mar 2022 15:17:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-concat-returning-null/m-p/25594#M17831</guid>
      <dc:creator>User16752245772</dc:creator>
      <dc:date>2022-03-15T15:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: SQL CONCAT returning null</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-concat-returning-null/m-p/25595#M17832</link>
      <description>&lt;P&gt;CONCAT is a function defined in the SQL standard and available across a wide variety of DBMS. With the exception of Oracle which uses VARCHAR2 semantic across the board, the function returns NULL on NULL input.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;CONCAT_WS() is not standard and is mostly popularized by Microsoft SQL Server, and MySQL. Both ignore NULL semantic.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So Databricks is merely following precedent in both cases.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 10:37:14 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-concat-returning-null/m-p/25595#M17832</guid>
      <dc:creator>BilalAslamDbrx</dc:creator>
      <dc:date>2022-03-21T10:37:14Z</dc:date>
    </item>
  </channel>
</rss>

