<?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 try_to_timestamp not working as expected in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/try-to-timestamp-not-working-as-expected/m-p/39783#M27070</link>
    <description>&lt;P&gt;try_to_timestamp should return NULL for following expression but it is throwing error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try_to_timestamp(
           '2019-02-28 23:59:59.000',
          'yyyy-MM-dd HH:mm:ss'
        )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understand above expression is wrong as the date format does not confirm to the datetime literal, but I expect a null instead of&amp;nbsp;&lt;SPAN&gt;INCONSISTENT_BEHAVIOR_CROSS_VERSION.PARSE_DATETIME_BY_NEW_PARSER error.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 13 Aug 2023 22:11:06 GMT</pubDate>
    <dc:creator>PriyanshuKumar</dc:creator>
    <dc:date>2023-08-13T22:11:06Z</dc:date>
    <item>
      <title>try_to_timestamp not working as expected</title>
      <link>https://community.databricks.com/t5/data-engineering/try-to-timestamp-not-working-as-expected/m-p/39783#M27070</link>
      <description>&lt;P&gt;try_to_timestamp should return NULL for following expression but it is throwing error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try_to_timestamp(
           '2019-02-28 23:59:59.000',
          'yyyy-MM-dd HH:mm:ss'
        )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understand above expression is wrong as the date format does not confirm to the datetime literal, but I expect a null instead of&amp;nbsp;&lt;SPAN&gt;INCONSISTENT_BEHAVIOR_CROSS_VERSION.PARSE_DATETIME_BY_NEW_PARSER error.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 22:11:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/try-to-timestamp-not-working-as-expected/m-p/39783#M27070</guid>
      <dc:creator>PriyanshuKumar</dc:creator>
      <dc:date>2023-08-13T22:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: try_to_timestamp not working as expected</title>
      <link>https://community.databricks.com/t5/data-engineering/try-to-timestamp-not-working-as-expected/m-p/39819#M27075</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/86399"&gt;@PriyanshuKumar&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thank you for your question in the Databricks community.&lt;/P&gt;&lt;P&gt;The&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;try_to_timestamp()&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function is designed to attempt to convert a string to timestamp format based on a specified pattern and return&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;null&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;if it fails to do so. However, in the scenario you provided, it is not returning&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;null&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;but is instead throwing an error due to the fact that the input timestamp string cannot be parsed by the new datetime parser introduced in newer versions of Apache Spark.&lt;/P&gt;&lt;P&gt;One way you can achieve your expected result is to use a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;try-catch&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;block instead of using&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;try_to_timestamp():&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql.functions import to_timestamp, lit

def try_to_timestamp(timestamp_string, pattern):
  try:
    return to_timestamp(lit(timestamp_string), pattern)
  except:
    return None

try_to_timestamp('2019-02-28 23:59:59.000', 'yyyy-MM-dd HH:mm:ss') # returns null&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This code defines a new function&amp;nbsp;&lt;/SPAN&gt;try_to_timestamp()&lt;SPAN&gt;&amp;nbsp;that wraps the&amp;nbsp;&lt;/SPAN&gt;to_timestamp()&lt;SPAN&gt;&amp;nbsp;function within a&amp;nbsp;&lt;/SPAN&gt;try-catch&lt;SPAN&gt;&amp;nbsp;block and returns&amp;nbsp;&lt;/SPAN&gt;None&lt;SPAN&gt;&amp;nbsp;in the event of an exception, effectively simulating the expected behavior that&amp;nbsp;&lt;/SPAN&gt;try_to_timestamp()&lt;SPAN&gt;&amp;nbsp;should have exhibited.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 07:26:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/try-to-timestamp-not-working-as-expected/m-p/39819#M27075</guid>
      <dc:creator>Kumaran</dc:creator>
      <dc:date>2023-08-14T07:26:45Z</dc:date>
    </item>
  </channel>
</rss>

