title: T-SQL Convert Arithmetic Formula into its Components
body: <p>Problem Statement:
  I have a Formula column that has the arithmetic operation in it. I want to extract the variable names from the formula, and delimit the variables with a comma and create a New Column &quot;Formula Components&quot;</p>
  <pre><code>The variable names follow the particular pattern - '%[^A-Za-z,_0-9 ]%' 
  </code></pre>
  <p>However, I also want to keep the &quot;Square Brackets&quot; if they are to appear in the formula.</p>
  <p>To Illustrate,</p>
  <p><strong>Input Data:</strong></p>
  <pre><code>ID    |    Formula
  ------|-------------------------------------------
  1     |    ([x1] + [x2]) / 100
  2     |    ([y1] - [x2]) * 100 
  3     |    z1 - z3 
  4     |    [z4] % z3 
  5     |    ((x1 * 2) + ((y1 + 2)/[x1])*[z3])/100   
  
  </code></pre>
  <p><strong>Desired Output</strong></p>
  <pre><code>ID    |    Formula                                |   FormulaComponents
  ------|------------------------------------------ |-----------------
  1     |   ([x1] + [x2]) / 100                     |  [x1],[x2]
  2     |   ([y1] - [x2]) * 100                     |  [y1],[x2]
  3     |   z1 - z3                                 |  [z1],[z3]
  4     |   [z4] % z3                               |  [z4],[z3]
  5     |   ((x1 * 2) + ((y1 + 2)/[x1])*[z3])/100   |  [x1],[y1],[z3]
  </code></pre>
  <p>As you can see above,</p>
  <ul>
  <li>Row 1. The Formula column consists of two variable, so the Formula
  components are [x1],[x2]</li>
  <li>Row 5. <strong>Note that x1 appears two times in the formula; Once as x1
  and once as [x1]. In this case, I only want to keep only [x1] once. [x1] could appear N number of
  times in the Formula Column, but should appear only once in the FormulaComponents Column</strong></li>
  </ul>
  <p>P.S.: <em>The Order of the variables appearing in the &quot;FormulaComponents&quot; column does not matter. So for example, in Row 5, the order can be [y1], [z3], [x1] OR [z3],[x1],[y1] and so on</em></p>
  <p>To summarize: I want to write a SELECT statement in T-SQL that will create this new column.</p>