title: Find date/financial quarter in file name (yyyy/mm/dd) then change date in file name to yyyy/mm/dd + 3 months
body: <p>Problem: there are 100+ tables to UNION ALL. The only difference in the name is the date. Instead of writing 100+ code blocks and only changing the date, it makes more sense to write a python script to output the sql code: </p>
  
  <p>Our SQL query, querying one table (i.e. one financial quarter): </p>
  
  <pre><code>SELECT 
    (SELECT
        PCR.repdte
        FROM 
        All_Reports_19921231_Performance_and_Condition_Ratios as 
        PCR) AS Quarter,
    (SELECT 
        Round(AVG(PCR.lnlsdepr))
        FROM 
        All_Reports_19921231_Performance_and_Condition_Ratios as 
        PCR) AS NetLoansAndLeasesToDeposits,
    (SELECT sum(CAST(LD.IDdepsam as int))
        FROM 
       'All_Reports_19921231_Deposits_Based_on_the_Dollars250,000
       _Reporting_Threshold' AS LD) AS 
       DepositAccountsWith$LessThan$250k
  </code></pre>
  
  <p>The naming convention of each file name includes the date (financial quarter)</p>
  
  <pre><code>All_Reports_19921231_Performance_and_Condition_Ratios
  All_Reports_19921231_Performance_and_Condition_Ratios
  All_Reports_19921231_Deposits_Based_on_the_Dollars250,000
  _Reporting_Threshold
  </code></pre>
  
  <p>We want to query all financial quarters including 19921231 to the present</p>
  
  <pre><code>    19921231
      19930331
      19930630 
      19930930
      19931231
      19940331
      19940630
      19940930
      19941231
      19950331
      19950630
      19950930
      19951231
       …..
       ….
      20180930
  </code></pre>
  
  <p>The script will: </p>
  
  <pre><code>Step one:  find the yyyy/mm/dd in the file name (e.g. 19921231)
  
  Step two:  copy the query
  
  Step three: change the yyyy/mm/dd in the copied file name(s)
  IF 1231 change to “+1”0331   (e.g. 19921231 to 19930331)
  IF 0331 change to 0630       (e.g. 19930331 to 19930630)
  IF 0630 change to 0930       (e.g. 19930630 to 19930930)
  IF 0930 change to 1231       (e.g. 19930930 to 19931231)
  IF 1231 change to +1 0331    (e.g. 19931231 to 19940331)
  …..
  …..
  …..
  IF 91231 change to 00331  (e.g. 19991231 to 20000331)
  ….
  IF 91231 change to 0031 (e.g. 20091231 to 20100331) 
  
  Step four: print new code block after UNION ALL 
  Step five: repeat step three
  Step six: repeat step four
  </code></pre>
  
  <p>The input would be a single financial quarter, (see above code block), with the output being, that code block repeated 100+ times, with only the yyyy/mm/dd changed in each file name.  Each code block will be joined with a UNION ALL: </p>
  
  <pre><code>SELECT 
    (SELECT
        PCR.repdte
        FROM 
        All_Reports_19921231_Performance_and_Condition_Ratios as 
        PCR) AS Quarter,
    (SELECT 
        Round(AVG(PCR.lnlsdepr))
        FROM 
        All_Reports_19921231_Performance_and_Condition_Ratios as 
        PCR) AS NetLoansAndLeasesToDeposits,
   (SELECT sum(CAST(LD.IDdepsam as int))
        FROM 
       'All_Reports_19921231_Deposits_Based_on_the_Dollars250,000
        _Reporting_Threshold' AS LD) AS 
        DepositAccountsWith$LessThan$250k
  UNION ALL
  SELECT 
    (SELECT
        PCR.repdte
        FROM 
        All_Reports_19930330_Performance_and_Condition_Ratios as 
        PCR) AS Quarter,
     (SELECT 
       Round(AVG(PCR.lnlsdepr))
       FROM All_Reports_19930330_Performance_and_Condition_Ratios 
       as PCR) AS NetLoansAndLeasesToDeposits,
     (SELECT sum(CAST(LD.IDdepsam as int))
       FROM 
      'All_Reports_19930330_Deposits_Based_on_the_Dollars250,000
       _Reporting_Threshold' AS LD) AS 
       DepositAccountsWith$LessThan$250k
  </code></pre>