title: Dynamic Substring in SQL
body: <p>I am working on a project where I have the two below tables:</p>
  
  <pre><code>#temp1:
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/XcZNo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XcZNo.png" alt="enter image description here"></a></p>
  
  <pre><code>#temp2:
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8UwwB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8UwwB.png" alt="enter image description here"></a></p>
  
  <p>So for every client on #temp2 table, I have to check if the substring(outcome,1,12) matches with text1/text2/text3/text4 of #temp1 table. And if they match the then concatenate text1/text2/text3/text4(replace the matching substring on #temp1 with outcome of #temp2). The final output is to be as below:</p>
  
  <pre><code>#output desired:
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tn8xv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tn8xv.png" alt="enter image description here"></a></p>
  
  <p>So for CID 1001, the substring(#temp2.outcome,1,12) = SOP201800205 matches with #temp1.text2 = SOP201800205. So my output should be</p>
  
  <pre><code>   Concat(#temp1.text1,#temp2.outcome)
  </code></pre>
  
  <p>My trial here in rextester demo:</p>
  
  <p><a href="https://rextester.com/CAEP69354" rel="nofollow noreferrer">https://rextester.com/CAEP69354</a></p>
  
  <p>Any help?!</p>