title: Incorrect syntax near the keyword CASE (SQL Function)
body: <p>I am porting a large Access application to SQL server, in doing so I have been working on re-writing all the VBA functions as SQL functions. I cannot get the following create function statement to execute. Seemingly I am using CASE incorrectly? Any help is appreciated, this is my first venture into SQL functions.</p>
  
  <pre><code>CREATE FUNCTION dbo.[NAL_PolicyCode]
  (
  @gm1 float, 
  @gm2 float = 1,
  @gm3 float = 1,
  @typ varchar(10) = ''
  )
  RETURNS VarChar(10)
  AS BEGIN
  DECLARE @gm float;
  SET @gm = @gm1;
  SET @gm = CASE 
      WHEN @gm2 &lt; @gm THEN @gm2 END 
  SET @gm = CASE
      WHEN @gm3 &lt; @gm THEN @gm3 END 
  
  SET @gm = Round(@gm, 3);
  
  IF(@typ = 'MS')
      BEGIN
          (CASE WHEN @gm &lt; 0.03 THEN 
                      PolicyCode = '1B'
               WHEN @gm &lt; 0.05 THEN
                      PolicyCode = '1C'
               WHEN @gm &lt; 0.08 THEN
                      PolicyCode = '1D'
               WHEN @gm &lt; 0.12 THEN
                      PolicyCode = '1E'
               WHEN @gm &lt; 0.16 THEN
                      PolicyCode = '1F'
               WHEN @gm &lt; 0.24 THEN
                      PolicyCode = '1G'
               WHEN @gm &lt; 0.29 THEN
                      PolicyCode = '1H'
               WHEN @gm &lt; 0.47 THEN
                      PolicyCode = '1J'
               ELSE
                      PolicyCode = '1K'
                      END) 
      END
  ELSE IF(@typ = 'PL')
      BEGIN
          (CASE WHEN @gm &lt; 0.35 THEN
                  PolicyCode = "8"
               WHEN @gm &lt; 0.45 THEN
                  PolicyCode = "P"
               WHEN @gm &lt; 0.58 THEN
                  PolicyCode = "V"
               WHEN @gm &lt; 0.7 THEN
                  PolicyCode = "4"
              ELSE
                  PolicyCode = "R"
                  END) 
      END
  ELSE
      BEGIN
          (CASE WHEN @gm &lt; 0.16 THEN
                  PolicyCode = "Y"
               WHEN @gm &lt; 0.24 THEN
                  PolicyCode = "Z"
               WHEN @gm &lt; 0.29 THEN
                  PolicyCode = "X"
              WHEN @gm &lt; 0.36 THEN
                  PolicyCode = "9"
               WHEN @gm &lt; 0.41 THEN
                  PolicyCode = "J"
               WHEN @gm &lt; 0.47 THEN
                  PolicyCode = "N"
               WHEN @gm &lt; 0.55 THEN
                  PolicyCode = "D"
               WHEN @gm &lt; 0.63 THEN
                  PolicyCode = "S"
               WHEN @gm &lt; 0.75 THEN
                  PolicyCode = "T"
              ELSE
                  PolicyCode = "U"
                  END) 
      END
  END;
  </code></pre>