<?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 How to convert column type from str to date in sparksql when the format is not yyyy-mm-dd? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29519#M21242</link>
    <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I imported a large csv file into databricks as a table. &lt;/P&gt;
&lt;P&gt;I am able to run sql queries on it in a databricks notebook.&lt;/P&gt;
&lt;P&gt;In my table, I have a column that contains date information in the mm/dd/yyyy format :&lt;/P&gt;
&lt;P&gt;12/29/2015&lt;/P&gt;
&lt;P&gt;12/30/2015 etc...&lt;/P&gt;
&lt;P&gt;Databricks imported this column with type &lt;B&gt;str&lt;/B&gt;, instead of &lt;B&gt;date&lt;/B&gt;. Forcing a 'timestamp' type in the Table UI did not have any effect.&lt;/P&gt;
&lt;P&gt;How can I convert this column type to a &lt;B&gt;date&lt;/B&gt; inside sql?&lt;/P&gt;
&lt;P&gt;I tried to do &lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;select cast(arrival_date as date) from my_data_table&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;however, this requires that the str column is in YYYY-mm-dd format. And mine is mm/dd/yyyy format as mentioned above.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;select to_date('15/1/09') as date;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;does not work either for the same reason.&lt;/P&gt;
&lt;P&gt;What can I do to have a column of dates?&lt;/P&gt;
&lt;P&gt;Is it true that SparkSQL does not support 'update' operations? In that case, I cannot rearrange the string to fit the format either? What options do I have?&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Aug 2016 04:29:07 GMT</pubDate>
    <dc:creator>semihcandoken</dc:creator>
    <dc:date>2016-08-19T04:29:07Z</dc:date>
    <item>
      <title>How to convert column type from str to date in sparksql when the format is not yyyy-mm-dd?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29519#M21242</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I imported a large csv file into databricks as a table. &lt;/P&gt;
&lt;P&gt;I am able to run sql queries on it in a databricks notebook.&lt;/P&gt;
&lt;P&gt;In my table, I have a column that contains date information in the mm/dd/yyyy format :&lt;/P&gt;
&lt;P&gt;12/29/2015&lt;/P&gt;
&lt;P&gt;12/30/2015 etc...&lt;/P&gt;
&lt;P&gt;Databricks imported this column with type &lt;B&gt;str&lt;/B&gt;, instead of &lt;B&gt;date&lt;/B&gt;. Forcing a 'timestamp' type in the Table UI did not have any effect.&lt;/P&gt;
&lt;P&gt;How can I convert this column type to a &lt;B&gt;date&lt;/B&gt; inside sql?&lt;/P&gt;
&lt;P&gt;I tried to do &lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;select cast(arrival_date as date) from my_data_table&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;however, this requires that the str column is in YYYY-mm-dd format. And mine is mm/dd/yyyy format as mentioned above.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;select to_date('15/1/09') as date;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;does not work either for the same reason.&lt;/P&gt;
&lt;P&gt;What can I do to have a column of dates?&lt;/P&gt;
&lt;P&gt;Is it true that SparkSQL does not support 'update' operations? In that case, I cannot rearrange the string to fit the format either? What options do I have?&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2016 04:29:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29519#M21242</guid>
      <dc:creator>semihcandoken</dc:creator>
      <dc:date>2016-08-19T04:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert column type from str to date in sparksql when the format is not yyyy-mm-dd?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29520#M21243</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;The following worked for me:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;df.withColumn("tx_date", to_date(unix_timestamp($"date", "M/dd/yyyy").cast("timestamp")))&lt;/CODE&gt;&lt;/PRE&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jul 2017 19:01:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29520#M21243</guid>
      <dc:creator>JoeConley</dc:creator>
      <dc:date>2017-07-31T19:01:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert column type from str to date in sparksql when the format is not yyyy-mm-dd?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29521#M21244</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;also to_date($"date", "format") works.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 16:12:02 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29521#M21244</guid>
      <dc:creator>Bill_Chambers</dc:creator>
      <dc:date>2017-08-02T16:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert column type from str to date in sparksql when the format is not yyyy-mm-dd?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29522#M21245</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I tried the above problem with a string and solved it as below:&lt;/P&gt;
&lt;P&gt;val df = sc.parallelize(Seq("08-26-2016")).toDF("Id") &lt;/P&gt;
&lt;P&gt;df.createOrReplaceTempView("table1") &lt;/P&gt;
&lt;P&gt;val bdf = spark.sql("""select from_unixtime(unix_timestamp(Id, 'MM-dd-yyyy')) as new_format from table1""")&lt;/P&gt;
&lt;P&gt; bdf.printSchema&lt;/P&gt;
&lt;P&gt; bdf.show &lt;/P&gt;
&lt;P&gt;val bbdf = bdf.withColumn("dt",$"new_format".cast("date")) &lt;/P&gt;
&lt;P&gt;bbdf.printSchema&lt;/P&gt;
&lt;P&gt;bbdf.show &lt;/P&gt;
&lt;P&gt;bbdf.select(year($"dt")).show&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2018 14:52:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29522#M21245</guid>
      <dc:creator>shalli</dc:creator>
      <dc:date>2018-01-26T14:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert column type from str to date in sparksql when the format is not yyyy-mm-dd?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29523#M21246</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;@josephpconley would it be safe to cast a column that contains null values?&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 04:37:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-column-type-from-str-to-date-in-sparksql-when-the/m-p/29523#M21246</guid>
      <dc:creator>ShubhamGupta187</dc:creator>
      <dc:date>2018-04-20T04:37:52Z</dc:date>
    </item>
  </channel>
</rss>

