I\'m wondering if I can select the value of a column if the column exists and just select null otherwise. In other words I\'d like to \"lift\" the select statement to handl
you can use dynamic SQL.
first you need to check exist column and then create dynamic query.
DECLARE @query NVARCHAR(MAX) = '
SELECT FirstColumn, SecondColumn, '+
(CASE WHEN exists (SELECT 1 FROM syscolumns
WHERE name = 'ColumnName' AND id = OBJECT_ID('TableName'))
THEN 'ColumnName'
ELSE 'NULL as ThreeColumn'
END) + '
FROM TableName'
EXEC sp_executesql @query;