title: T-SQL query is hanging
body: <p>First off, I didn't write this query. It's created dynamically in code I inherited. It works 99.9999% of the time so I'd prefer not to touch it. However. The following query just hangs:</p>
  <pre><code>SELECT
      *
  FROM
      (
          SELECT TOP 1
              Track
              , RequestID
              , ClientName
              , MatterName
              , CASE
                    WHEN PATINDEX('% - Yellow', PhaseName) &gt; 0 THEN REPLACE(PhaseName, ' - Yellow', '')
                    ELSE PhaseName
                END                 AS PhaseName
              , MPApprovalStatusName
              , DateOpened
              , RequestorName
              , Notes
              , IsNewClient
              , AffiliatedClients
          FROM
              (
                  SELECT TOP 15
                      *
                  FROM
                      vwRequests AS T1
                  WHERE
                      ClientName LIKE '%harch%'
                  ORDER BY
                      DateOpened DESC
              ) AS T2
          ORDER BY
              DateOpened ASC
      ) AS T3
  ORDER BY
      DateOpened DESC
  </code></pre>
  <p>If I replace &quot;harch&quot; in the LIKE statement with &quot;chameleon&quot; (both should return results), results are returned with no problem. If I change the LIKE '%harch%' to = '(complete client name)', it works. If I leave LIKE, but remove the CASE statement and just output the PhaseName, it works. I experimented and did this:</p>
  <pre><code>SELECT TOP 15
      *
      , PATINDEX('% - Yellow', PhaseName)
  FROM
      vwRequests AS T1
  WHERE
      ClientName LIKE '%harch%'
  ORDER BY
      DateOpened DESC
  </code></pre>
  <p>It works fine. However, when I do this:</p>
  <pre><code>SELECT TOP 15
      *
      , PATINDEX('% - Yellow', PhaseName)
      , REPLACE(PhaseName, ' - Yellow', '')
  FROM
      vwRequests AS T1
  WHERE
      ClientName LIKE '%harch%'
  ORDER BY
      DateOpened DESC
  </code></pre>
  <p>it hangs. What's weird about this is that PhaseName in this case is &quot;Completed.&quot; Why would the REPLACE statement cause a problem? And why would LIKE be problematic in this case but not others? I'm on SQL Server 2017. Thanks in advance.</p>