title: select column's values if columns are not present in table in sql
body: <p>I have a table "ExcelData" like below :</p>
  
  <pre><code>Id   [Title 1]   [Title 2]    [Address 1]  [Address 2]    City  State
  1      A1          A2           add1         add2         X      Y
  2      B1          B2           add1         add2         X      Y
  </code></pre>
  
  <p>I need a query in string form which gets the result from above table after checking each column if it exists in table or not.</p>
  
  <pre><code>Declare @strSQL nvarchar(max), @RecId int
  set @RecId = 1
  </code></pre>
  
  <p>--- while loop will be there for @Count variable. My query is : </p>
  
  <pre><code>set @strSQL = 'SELECT CASE WHEN EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='ExcelData' and COLUMN_NAME='[Title ' + CONVERT(nvarchar(10), @Count)) THEN(SELECT '[Title ' + CONVERT(nvarchar(10), @Count)]'
  FROM ExcelData where ExcelData.Id = '+ @RecId +') ELSE NULL END AS [Title]
  SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='ExcelData' and COLUMN_NAME='[Email]' THEN(SELECT '[Email]' FROM ExcelData where ExcelData.Id = '+ @RecId +') ELSE NULL END AS [Email])'
  
  Execute(@strSQL)
  </code></pre>
  
  <p>I need result if column is exist in table then value of that column and if it is not then query should return null in the column value like below:</p>
  
  <p>Desired Result :</p>
  
  <pre><code>[Title 1]  [Title 2]  [Email]
     A1        A2        NULL
  </code></pre>
  
  <p>But I am not getting the desired result.</p>