Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2003
    Posts
    59
    Provided Answers: 1

    Question Unanswered: Querying with CASE and if a column exists or not without being an Invalid column

    Here is my current SQL Server query:
    Code:
    SELECT 
         COUNT(*) over () as countNum,
         [F1] AS STANDARDandOBJ,
         [F2] AS CLUSTER,
         [F3] AS OBJECTIVE,
         [F4] AS EXTRA0,
         CASE 
            WHEN COL_LENGTH('[tmpExcelDB].[dbo].[''Nursing$'']', [F5]) IS NULL 
            THEN 'NO' ELSE CONCAT([F1], [F2]) 
         END AS COMBINEF1F2 
    FROM 
        [tmpExcelDB].[dbo].['Nursing$'] 
    WHERE 
        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?

  2. #2
    Join Date
    Oct 2010
    Location
    Atlanta, GA
    Posts
    211
    Provided Answers: 1
    Quote Originally Posted by StealthRT View Post
    Here is my current SQL Server query:
    Code:
    SELECT 
         COUNT(*) over () as countNum,
         [F1] AS STANDARDandOBJ,
         [F2] AS CLUSTER,
         [F3] AS OBJECTIVE,
         [F4] AS EXTRA0,
         CASE 
            WHEN COL_LENGTH('[tmpExcelDB].[dbo].[''Nursing$'']', [F5]) IS NULL 
            THEN 'NO' ELSE CONCAT([F1], [F2]) 
         END AS COMBINEF1F2 
    FROM 
        [tmpExcelDB].[dbo].['Nursing$'] 
    WHERE 
        LOWER(F3) NOT LIKE 'planning tools-%'
    This works just fine as long as the table has F5 within it. Otherwise, it throws an error:



    How can I modify the above query if there's an F5 column or when there's not going to be one?
    can you provide some sample data and expected output?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •