title: Including a sometimes missing column in TSQL if it's there (or alternative if it's not)
body: <p>I have a weird table that I have to deal with that sometimes has a disappearing column. If the column is there, I need to use it. But if not, I need to account for that and use an alternative. But when I try this code when the column is missing, SSMS throws an error (Invalid column name 'DOB'). Shouldn't this short circuit if the column isn't there and never get to the part where it calls the column? So why the error message? Any solutions? Thanks in advance for any help!</p>
  
  <pre><code>SELECT
      SalesClients.ClientName,
      (CASE 
          WHEN (COL_LENGTH('dbo.SalesClients', 'DOB') IS NULL) 
             THEN DATEADD(month, -SalesClients.AgeInMonth, GETDATE())
          WHEN SalesClients.DOB IS NULL 
             THEN DATEADD(month, -SalesClients.AgeInMonth, GETDATE())
          ELSE SalesClients.DOB
       END) AS DOB
  FROM 
      dbo.SalesClients AS SalesClients
  </code></pre>