title: SQL - Replace non numeric characters in string
body: <p>I'm writing a function that takes a string and has to replace any non-numeric character with two numeric characters taken from a table. This is the code I use to explore the string and find the non-numeric characters:</p>
  
  <pre><code>SET @string = '1a2b3c4d'
  SET @wcount= 0
  SET @index = 1
  SET @len= LEN(@string)
  
  WHILE @index&lt;= @len
   BEGIN
      SET @char = SUBSTRING(@string, @index, 1)
      IF @char LIKE '%[a-z]%'
        PRINT 'char ' + CONVERT(varchar(10), @index)
      ELSE
        PRINT @char
      SET @index= @index+ 1
   END
  </code></pre>
  
  <p>The output is the following</p>
  
  <pre><code>1
  char 2
  2
  char 4
  3
  char 6
  4
  char 8
  </code></pre>
  
  <p>Now, when I find a non-numeric character I have to replace it with two numeric chars taken from a table by a select. E.g.</p>
  
  <pre><code>SELECT @temp = REPLACEMENT FROM Conversion_Tab WHERE EXPR = @char
  </code></pre>
  
  <p>In conclusion, if I have the following string '1a2a3a4a' and the replacement for 'a' is '88' the resulting string should be '188288388488'</p>
  
  <p>Thanks in advance for any help.</p>
  
  <p>Bye!</p>