title: How to escape charters in SQL Server?
body: <p>I need a SQL function or query that can escape '\' (backslash) but only odd numbers of '\' (backslashes) from string. </p>
  
  <p>For example:</p>
  
  <pre><code>DECLARE @str nvarchar(max) ='abc\df\\gh\\\eg\\\\yu'
  </code></pre>
  
  <p>I am using the SQL <code>Replace</code> function to escape the backslash (<code>\</code>) like this:</p>
  
  <pre><code>@str  = Replace(@str,'\','\\')
  </code></pre>
  
  <p>But the problem is it will replace the even numbers of sub strings too. But I only need it should replace only even number of sub strings having '\' from string.</p>