title: Last Quarter End-Date in Spark SQL
body: <p>I have a SQL-Server query that calculates the <strong>last quarter End-date</strong>. Now, I need to convert this query into a <code>Spark-SQL</code> query.</p>
  <p>The <code>Sql-Server</code> query and some sample examples are:</p>
  <pre><code>select dateadd(dd, -1, dateadd(qq, datediff(qq, 0, cast('2020-09-09' AS DATE)), 0)) as end_dt
  </code></pre>
  <p>Output: <code>2020-06-30</code></p>
  <p>To calculate the same for another (<code>2021-01-10</code>) date:</p>
  <pre><code>select dateadd(dd, -1, dateadd(qq, datediff(qq, 0, cast('2021-01-10' AS DATE)), 0)) as end_dt
  </code></pre>
  <p>Output: <code>2020-12-31</code></p>
  <p><strong>Question</strong>: How can I <strong>implement</strong> the same in <strong>Spark SQL query</strong> (v2.4) ? How do I calculate the <strong>last day</strong> of the <strong>previous</strong> quarter as per the <code>SQL-Server</code> examples above and the required output below ?</p>
  <pre><code>input_date | end_date
  ------------------------
  2020-01-21 | 2019-12-31
  2020-02-06 | 2019-12-31
  2020-04-15 | 2020-03-31
  2020-07-10 | 2020-06-30
  2020-10-20 | 2020-09-30
  2021-02-04 | 2020-12-31
  </code></pre>
  <p>Any help is appreciated. Thanks.</p>