title: T-SQL - Why does REPLACE not work properly within Case?
body: <p>See this SQL Fiddle: <a href="http://sqlfiddle.com/#!3/068e9/3/0" rel="nofollow">http://sqlfiddle.com/#!3/068e9/3/0</a><br>
  (MS Sql Server 2008)</p>
  
  <p>Setup of schema is:</p>
  
  <pre><code>create table test_replace ( something varchar(50));
  
  insert into test_replace(something) values('072 782 5343');
  insert into test_replace(something) values('071 647 2342');
  insert into test_replace(something) values('whatever@something.com');
  </code></pre>
  
  <p>Query is:</p>
  
  <pre><code>select case when CHARINDEX('@', something,0) &gt; -1
  then REPLACE(something,'@','@testtesttest')
  else REPLACE(something,' ','') end as test1,
  REPLACE(something,' ','') as test2
  from test_replace
  </code></pre>
  
  <p>The second REPLACE in the CASE statement does not work (compare output of test1 to test2 when applied to the phone numbers). Why is that?</p>