title: SQL Server : basic if condition in function
body: <p>I have problem with basic <code>if</code> condition in function.. I have something like this</p>
  
  <pre><code>IF  LEFT(@text, 1) = '#'
          BEGIN
              /* trim first character if is '#' */
              RETURN RIGHT(@text,LEN(@text)-1)
          END
      ELSE
          BEGIN
              RETURN @text
          END
      END
  </code></pre>
  
  <p>Console output show me this</p>
  
  <blockquote>
    <p>Incorrect syntax near the keyword 'IF'. 
    Incorrect syntax near the keyword 'END'.  </p>
  </blockquote>
  
  <p>I wonder where the problem is. Can I simplify the if condition like this?</p>
  
  <pre><code>IF  LEFT(@text, 1) = '#'  RETURN RIGHT(@text,LEN(@text)-1)
  ELSE RETURN @text
  </code></pre>
  
  <p>Thank you</p>
  
  <p>edit: now I have that if in function</p>
  
  <pre><code>CREATE FUNCTION Trimming (@text VARCHAR(255))
  RETURNS VARCHAR(255)
  AS
  BEGIN
      DECLARE @TrimText AS VARCHAR(255) ;
  
      SET @TrimText=LTRIM(RTRIM(@text)
      IF  LEFT(@TrimText, 1) = '#'
          BEGIN
              RETURN RIGHT(@TrimText,LEN(@TrimText)-1)
          END
      ELSE
          BEGIN
              RETURN @TrimText
          END
  END
  </code></pre>
  
  <p>and console says </p>
  
  <blockquote>
    <p>Incorrect syntax near the keyword 'IF'.  </p>
  </blockquote>