1

I have a table with a named column but no rows.

Col1

I wanted to write a select similar to:

SELECT TOP(1)
    NULLIF(Col1, '')
FROM table

I wanted to get a result like:

   Col1
1  NULL

But instead I get just the Col1 name and no results. How can I get a table with NULL as the result?

3
  • 1
    FYI, a TOP without an ORDER BY will return arbitrary rows. They could easily be different every time you run the query. Are you saying you want a random value in the column Col1 from your table table? Commented May 18, 2021 at 15:45
  • Your question is vague because it doesn't specify what to do if there are rows. Commented May 18, 2021 at 15:46
  • The specific assumption is that the table is empty. In this case, I have something different to handle if there are rows. This is more of a general question for empty tables Commented May 18, 2021 at 16:00

1 Answer 1

1

You can use aggregation:

    select max(col1)
    from t;

An aggregation query with no group by or having always returns one row. If there are no rows then the value is (generally) NULL for aggregation functions (the exception is COUNT()).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.