<?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 SQL cast operator not working properly in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/sql-cast-operator-not-working-properly/m-p/28594#M20374</link>
    <description>&lt;P&gt;please have a look at the attached screenshot&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Three strings converted to float, each resulting in the same number. &lt;/P&gt;&lt;P&gt;22015683.000000000000000000 =&amp;gt; 22015684&lt;/P&gt;&lt;P&gt;22015684.000000000000000000 =&amp;gt; 22015684&lt;/P&gt;&lt;P&gt;22015685.000000000000000000 =&amp;gt; 22015684&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="sql_cast"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2105i59F43B8336BB8E44/image-size/large?v=v2&amp;amp;px=999" role="button" title="sql_cast" alt="sql_cast" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Feb 2022 18:56:05 GMT</pubDate>
    <dc:creator>my_community2</dc:creator>
    <dc:date>2022-02-11T18:56:05Z</dc:date>
    <item>
      <title>SQL cast operator not working properly</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-cast-operator-not-working-properly/m-p/28594#M20374</link>
      <description>&lt;P&gt;please have a look at the attached screenshot&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Three strings converted to float, each resulting in the same number. &lt;/P&gt;&lt;P&gt;22015683.000000000000000000 =&amp;gt; 22015684&lt;/P&gt;&lt;P&gt;22015684.000000000000000000 =&amp;gt; 22015684&lt;/P&gt;&lt;P&gt;22015685.000000000000000000 =&amp;gt; 22015684&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="sql_cast"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2105i59F43B8336BB8E44/image-size/large?v=v2&amp;amp;px=999" role="button" title="sql_cast" alt="sql_cast" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 18:56:05 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-cast-operator-not-working-properly/m-p/28594#M20374</guid>
      <dc:creator>my_community2</dc:creator>
      <dc:date>2022-02-11T18:56:05Z</dc:date>
    </item>
    <item>
      <title>Re: SQL cast operator not working properly</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-cast-operator-not-working-properly/m-p/28596#M20376</link>
      <description>&lt;P&gt;Hi @Maciej G​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I guess, this has something to do with the &lt;A href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format" alt="https://en.wikipedia.org/wiki/Single-precision_floating-point_format" target="_blank"&gt;&lt;B&gt;data type FLOAT&lt;/B&gt;&lt;/A&gt; and its precision.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Floats are only an approximation with a given precision. Either you should consider using date type &lt;B&gt;DOUBLE &lt;/B&gt;(double precision compared to FLOAT)  - or, if you know upfront the number of digits before and after the decimal&amp;nbsp;separator of the values to be expected use DECIMAL data type.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%%sql
WITH test_setup AS (
  SELECT '22015683.000000000000000000' AS `string`
  UNION ALL
  SELECT '22015684.000000000000000000' AS `string`
  UNION ALL
  SELECT '22015685.000000000000000000' AS `string`
)
SELECT
    `string`
  , cast(`string` AS float)            AS `float`
  , cast(`string` AS double)           AS `double`
  , cast(`string` AS decimal(26,18))   AS `decimal`
  , cast(`string` AS integer)          AS `integer`
FROM
  test_setup&lt;/CODE&gt;&lt;/PRE&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/2095iEF849468BF69F7FD/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Feb 2022 21:17:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-cast-operator-not-working-properly/m-p/28596#M20376</guid>
      <dc:creator>MartinB</dc:creator>
      <dc:date>2022-02-14T21:17:41Z</dc:date>
    </item>
  </channel>
</rss>

