Unanswered: Querying with CASE and if a column exists or not without being an Invalid column
Here is my current SQL Server query:
COUNT(*) over () as countNum,
[F1] AS STANDARDandOBJ,
[F2] AS CLUSTER,
[F3] AS OBJECTIVE,
[F4] AS EXTRA0,
WHEN COL_LENGTH('[tmpExcelDB].[dbo].[''Nursing$'']', [F5]) IS NULL
THEN 'NO' ELSE CONCAT([F1], [F2])
END AS COMBINEF1F2
LOWER(F3) NOT LIKE 'planning tools-%'
This works just fine as long as the table has F5 within it. Otherwise, it throws an error:
Invalid column name 'F5'.
How can I modify the above query if there's an F5 column or when there's not going to be one?