title: Recursion to pull multiple substrings from an image file
body: <p>I'm working with a database that for each entry has a list of parameters formatted in a uniform fashion (in a text file), but than jammed into a single field as an image.</p>
  
  <p>I need query to extract multiple substrings from this image. The criteria is this:
  If I'm pulling information for machine 8, I want every string that starts with
  '[08', and I want every other character until the carriage return.
  This code successfully extracts everything, but I don't know how to use a where clause on modified data.</p>
  
  <pre><code>DECLARE @POS INT
  SET @POS = 0
  DECLARE @PSU VARCHAR(12)
  SET @PSU ='PSU-1.4'
  
  WHILE (@POS &lt; 1000) BEGIN
      SELECT SUBSTRING( CONVERT( VARCHAR( MAX)
                               , CONVERT( BINARY(8000)
                                        , BITS
                                        )
                               )
                      , @POS --SUBSTRING START
                      , CHARINDEX( CHAR(13)
                                 , CONVERT( VARCHAR(MAX)
                                          , CONVERT( BINARY(8000)
                                                   , BITS
                                                   )
                                          )
                                 , @POS --CHARINDEX OFFSET
                                 ) - @POS --SHOULD BE THE SAME AS THE SUBSTRING
                      )
          AS [PARAMETER]
        FROM PART_BINARY
       WHERE PART_ID = @PSU --AND PARAMETER LIKE '[08%'
      SET @POS = 2 + (SELECT @POS + CHARINDEX( CHAR(13)
                                             , CONVERT( VARCHAR(MAX)
                                                      , CONVERT( BINARY(8000)
                                                               , BITS
                                                               )
                                                      )
                                             , @POS
                                             ) - @POS
                        FROM PART_BINARY
                       WHERE PART_ID = @PSU
                     )
  END
  GO
  </code></pre>
  
  <p>Below is a sample of some of converted text in the field that I would be parsing. </p>
  
  <pre><code>[41 Platen Seal Temp Nominal]           {275}   °F
  [41 Platen Seal Temp High Limit]        {284}   °F
  [41 Platen Seal Temp Low Limit]         {270}   °F
  [41 Aux Seal Temp Nominal]              {275}   °F
  [41 Aux Seal Temp High Limit]           {284}   °F
  [41 Aux Seal Temp Low Limit]            {270}   °F
  [41 Platen Base Heat]                   {1}     On
  [41 Dwell Main Nominal]                 {1.1}   sec
  [41 Dwell Main High Limit]              {1.3}   sec
  [41 Dwell Main Low Limit]               {0.9}   sec
  [41 Dwell Aux Nominal]                  {1.1}   sec
  [41 Dwell Aux High Limit]               {1.3}   sec
  [41 Dwell Aux Low Limit]                {0.9}   sec
  [41 Pressure Main Nominal]              {}      
  [41 Pressure Main High Limit]           {}      
  [41 Pressure Main Low Limit]            {}      
  [41 Pressure Aux Nominal]               {}      
  [41 Pressure Aux High Limit]            {}      
  [41 Pressure Aux Low Limit]             {}      
  #41 Teflon (top/bottom):                NONE / COARSE
  #41 Rubber:                             2x 1/8" 65D
  
  [76 Platen Seal Temp Nominal]           {285}   °F
  [76 Platen Seal Temp High Limit]        {300}   °F
  [76 Platen Seal Temp Low Limit]         {280}   °F
  [76 Platen Base Heat]                   {1}     On
  [76 Dwell Main Nominal]                 {0.67}  sec
  [76 Dwell Main High Limit]              {0.84}  sec
  [76 Dwell Main Low Limit]               {0.56}  sec
  [76 Pressure Main Nominal]              {}      
  [76 Pressure Main High Limit]           {}      
  [76 Pressure Main Low Limit]            {}      
  #76 Teflon (top/bottom):                NONE / COARSE
  #76 Rubber:                             2x 1/8" 65D
  </code></pre>