title: SQL INSERT with conditional statement IF THEN ELSE
body: <p>I am trying to MERGE values from one table to another. One of the values is a conditional value, but it looks like I am not getting the syntax correctly. Initially, I was using an IF-THEN-ELSE statement but was advise to use a CASE statement instead.</p>
  
  <p>Here is the gist the syntax that is failing:</p>
  
  <pre><code>CREATE PROCEDURE EmployeeMerge
  AS 
  BEGIN
      SET NOCOUNT ON;
      MERGE INTO Employee AS t1  
      USING 
          (SELECT 
                 EmployeeName,
                 Zip,
                 UpdateDate 
          FROM table2) AS t2
          ON (t1.EmployeeID = t2.EmployeeID)
      WHEN MATCHED AND t2.UpdatedDate &gt; t1.UpdatedDate THEN 
          UPDATE 
          SET 
              t1.EmployeeName = s.EmployeeName, 
              t1.Zip =
                  (CASE 
                      WHEN t2.ZipExt IS NULL OR t2.ZipExt = '' THEN t2.Zip 
                      ELSE (t2.Zip + '-' + t2.ZipExt)
                  END),
              t1.UpdatedDate = t2.UpdateDate
      WHEN NOT MATCHED THEN
          INSERT (EmployeeName,  
              Zip,  
              ModDate)
          VALUES 
              (t2.Name, 
              (CASE 
                  WHEN t2.ZipExt IS NULL OR t2.ZipExt = '' THEN t2.Zip 
                  ELSE (t2.Zip + '-' + t2.Zip_Ext)
              END),
              t2.UpdatedDate)
      OUTPUT 
          deleted.*, 
          $action, 
          inserted.* 
  END; **-- A MERGE statement must be terminated by a semi-colon (;)**
  GO
  </code></pre>
  
  <p>This MERGE statement works just fine if I do not implement the condition, i.e. simply set the t1.Zip = t2.Zip, but of course, this is avoiding the t2.ZipExt field.</p>