title: SQL Server : Case Statement in function doesn't output correctly
body: <p>I want to create a function to shorten the query in below</p>
  <p>Query:</p>
  <pre><code>SELECT 
      CASE 
          WHEN SUBS_ARPU_M1 IS NULL 
              THEN 'Missing'
          WHEN SUBS_ARPU_M1 &gt; 0 AND SUBS_ARPU_M1 &lt;= 5 
              THEN '&gt; 0 - &lt;= 5'   
          WHEN SUBS_ARPU_M1 &gt; 5 AND SUBS_ARPU_M1 &lt;= 12 
              THEN '&gt; 5 - &lt;= 12'
          WHEN SUBS_ARPU_M1 &gt; 12 AND SUBS_ARPU_M1 &lt;= 25 
              THEN '&gt; 12 - &lt;=25'
          WHEN SUBS_ARPU_M1 &gt; 25 
              THEN '&gt; 25'
      END AS ARPU_GRP, 
      COUNT(*) 'Toplam Müşteri Sayısı'
  FROM
      TRAIN
  GROUP BY 
      CASE 
          WHEN SUBS_ARPU_M1 IS NULL 
              THEN 'Missing'
          WHEN SUBS_ARPU_M1 &gt; 0 AND SUBS_ARPU_M1 &lt;= 5 
              THEN '&gt; 0 - &lt;= 5'   
          WHEN SUBS_ARPU_M1 &gt; 5 AND SUBS_ARPU_M1 &lt;= 12 
              THEN '&gt; 5 - &lt;= 12'
          WHEN SUBS_ARPU_M1 &gt; 12 AND SUBS_ARPU_M1 &lt;= 25 
              THEN '&gt; 12 - &lt;=25'
          WHEN SUBS_ARPU_M1 &gt; 25 
              THEN '&gt; 25'
      END 
  </code></pre>
  <p>OUTPUT:</p>
  <pre><code>ARPU_GRP    Toplam Müşteri Sayısı
  ----------------------------------
  &gt; 5 - &lt;=12  1565
  &gt; 0 - &lt;=5   1318
  &gt; 25        678
  Missing     255
  &gt; 12 - &lt;=25 1184
  </code></pre>
  <p>But when I create a function from the case statement part of the query above, output changes. Normally this function always works when I use between and method, but not with this one.</p>
  <p>Function:</p>
  <pre><code>CREATE FUNCTION ARPU_GRP
      (@Arpu INT)
  RETURNS VARCHAR(MAX)
  AS
  BEGIN
      RETURN
          CASE 
              WHEN @Arpu IS NULL THEN 'Missing'
              WHEN @Arpu &gt; 0 AND @Arpu &lt;= 5 THEN '&gt; 0 - &lt;=5'  
              WHEN @Arpu &gt; 5 AND @Arpu &lt;= 12 THEN '&gt; 5 - &lt;=12'
              WHEN @Arpu &gt; 12 AND @Arpu &lt;= 25 THEN '&gt; 12 - &lt;=25'
              WHEN @Arpu &gt; 25 THEN '&gt; 25'
          END 
  END
  </code></pre>
  <p>Query:</p>
  <pre><code>SELECT 
      dbo.ARPU_GRP(SUBS_ARPU_M1) AS ARPU_GRP, 
      COUNT(*) 'Toplam Müşteri Sayısı'
  FROM
      TRAIN
  GROUP BY 
      dbo.ARPU_GRP(SUBS_ARPU_M1)
  </code></pre>
  <p>Output:</p>
  <pre><code>ARPU_GRP    Toplam Müşteri Sayısı
  ----------------------------------
  NULL        208
  &gt; 0 - &lt;=5   1392
  &gt; 12 - &lt;=25 1053
  &gt; 25        645
  &gt; 5 - &lt;=12  1447
  Missing     255
  </code></pre>