<?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>question spark sql transformation of string to timestamp in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/spark-sql-transformation-of-string-to-timestamp/m-p/186290#M67933</link>
    <description>&lt;P&gt;I am running this query through beeline and it transforms my string value from string to timestamp successfully.&lt;/P&gt;&lt;P&gt;select cast(regexp_replace(createdatetime,'(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})Z','$1-$2-$3 $4:$5:$6.$7') as timestamp) as thetime  from L2_view where load_dt='20170908' and createdatetime is not null;&lt;/P&gt;&lt;P&gt;When I run the same query through spark sqlContext I get nulls.&lt;/P&gt;&lt;P&gt;sqlContext.sql("select cast(regexp_replace(createdatetime,'(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})Z','$1-$2-$3 $4:$5:$6.$7') as timestamp) as thetime  from L2_view where load_dt='20170908' and createdatetime is not null").show&lt;/P&gt;&lt;P&gt;+-------+
|thetime|
+-------+
+-------+&lt;/P&gt;&lt;P&gt;Can you explain why this happens?  I am running Spark 1.6.&lt;/P&gt;</description>
    <pubDate>Wed, 13 Sep 2017 03:18:26 GMT</pubDate>
    <dc:creator>kacreed</dc:creator>
    <dc:date>2017-09-13T03:18:26Z</dc:date>
    <item>
      <title>spark sql transformation of string to timestamp</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/spark-sql-transformation-of-string-to-timestamp/m-p/186290#M67933</link>
      <description>&lt;P&gt;I am running this query through beeline and it transforms my string value from string to timestamp successfully.&lt;/P&gt;&lt;P&gt;select cast(regexp_replace(createdatetime,'(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})Z','$1-$2-$3 $4:$5:$6.$7') as timestamp) as thetime  from L2_view where load_dt='20170908' and createdatetime is not null;&lt;/P&gt;&lt;P&gt;When I run the same query through spark sqlContext I get nulls.&lt;/P&gt;&lt;P&gt;sqlContext.sql("select cast(regexp_replace(createdatetime,'(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})Z','$1-$2-$3 $4:$5:$6.$7') as timestamp) as thetime  from L2_view where load_dt='20170908' and createdatetime is not null").show&lt;/P&gt;&lt;P&gt;+-------+
|thetime|
+-------+
+-------+&lt;/P&gt;&lt;P&gt;Can you explain why this happens?  I am running Spark 1.6.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2017 03:18:26 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/spark-sql-transformation-of-string-to-timestamp/m-p/186290#M67933</guid>
      <dc:creator>kacreed</dc:creator>
      <dc:date>2017-09-13T03:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: spark sql transformation of string to timestamp</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/spark-sql-transformation-of-string-to-timestamp/m-p/186291#M67934</link>
      <description>&lt;P&gt;&lt;A rel="user" href="https://community.cloudera.com/users/31179/kacreed.html" nodeid="31179"&gt;@kenny creed&lt;/A&gt;&lt;/P&gt;&lt;P&gt;you are using regexp_replace in spark which gives you string datatype. In spark you have to use cast to convert it. Given below an example which might help in solving your problem:&lt;/P&gt;&lt;P&gt;Hope it helps!&lt;/P&gt;&lt;PRE&gt;val res = df.select($"id", $"date", unix_timestamp($"date", "yyyy/MM/dd HH:mm:ss").cast(TimestampType).as("timestamp"), current_timestamp(), current_date())
&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:21:00 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/spark-sql-transformation-of-string-to-timestamp/m-p/186291#M67934</guid>
      <dc:creator>balavignesh_nag</dc:creator>
      <dc:date>2017-09-15T15:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: spark sql transformation of string to timestamp</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/spark-sql-transformation-of-string-to-timestamp/m-p/186292#M67935</link>
      <description>&lt;P&gt;Perfect, thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 23:24:57 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/spark-sql-transformation-of-string-to-timestamp/m-p/186292#M67935</guid>
      <dc:creator>kacreed</dc:creator>
      <dc:date>2017-09-15T23:24:57Z</dc:date>
    </item>
  </channel>
</rss>

