title: How to update a table if a column exists in SQL Server?
body: <p>I have a table MyTable created by</p>
  
  <pre><code>CREATE TABLE MyTable
  (
      [ID] [bigint] IDENTITY(1,1) NOT NULL,
      [Type] [int] NOT NULL,
      [CreatedDate] [datetime] NOT NULL,
      [ModifiedDate] [datetime] NOT NULL,
  )
  </code></pre>
  
  <p>I want to check if a column exists in my table, and if it does, I want to copy the data to a different column, then drop the old column, like this:</p>
  
  <pre><code>IF (SELECT COLUMNPROPERTY(OBJECT_ID('MyTable'), 'Timestamp', 'Precision')) IS NOT NULL
  BEGIN
      UPDATE [dbo].[MyTable]
      SET [CreatedDate] = [Timestamp]
  
      ALTER TABLE [dbo].[MyTable]
      DROP COLUMN [Timestamp]
  END
  GO
  </code></pre>
  
  <p>However, when I try to run this I get an error:</p>
  
  <pre><code>Invalid column name 'Timestamp'
  </code></pre>
  
  <p>How can I accomplish what I'm trying to do?</p>