0

I've got the next query

Select LAST_NAME from test.[USER]
        Where ID = (Select test_support from test.UPGRADE
        Where KEYED_NAME Like '%abc%'
        and STATE = 'Active')

The result like:

 LastName1

But if the Keyed_Name doesn't exist, the result is empty. I get the empty column:

enter image description here

How to change it to receive some other value like '-' instead empty.

So if the query result is empty, I will receive:

enter image description here

I tried next query

DECLARE @EmptyString NVARCHAR( 10 ) = ''; 
        Select CASE WHEN LAST_NAME <> @EmptyString THEN LAST_NAME ELSE '-' END
        from test.[USER]
        Where ID = (Select test_support from test.UPGRADE
        Where KEYED_NAME Like '%abc%'
        and STATE = 'Active')

but it works only for case when the string is not empty.

3
  • meta.stackoverflow.com/questions/388759/… Commented Dec 14, 2022 at 10:20
  • So, the query gives you all matching last names (which can be zero, one or many rows), but in case there is no match, you want a dummy result row with '-'? Commented Dec 14, 2022 at 10:25
  • What is your DBMS? Please always tag your SQL requests with your DBMS. We may need to know this for our answers. Commented Dec 14, 2022 at 10:27

2 Answers 2

1

We can put the whole query in COALESCE and take '-' if the query doesn't find records:

SELECT COALESCE(
(SELECT LAST_NAME from users
        Where ID = (SELECT test_support FROM upgrade
        Where KEYED_NAME LIKE '%abc%'
        AND state = 'Active')), '-') AS LAST_NAME;

Try out here: db<>fiddle

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

3 Comments

This will crash when there is more then one user Where KEYED_NAME LIKE '%abc%'.
@ThorstenKettner True. As I understood the question, the query will always fetch one entry only, so this would not happen. But the requirement is not 100% clear, we could also understand it in another way. So I think - if this is a possible risk in OP's situation - they should edit the question and explain what to do in this case (if it's enough to fetch one of these rows or if all should be selected). In my opinion, in this case doing this in the application or in a function should be prefered rather than in a query. Still using a query for that will likely lead to very poor readability.
@JonasMetzler Thanks! This will work because the only 1 user could be in the result, in my case state active makes it
0

This should do it:

select top 1 * from
(Select LAST_NAME from test.[USER]
        Where ID = (Select test_support from test.UPGRADE
        Where KEYED_NAME Like '%abc%'
        and STATE = 'Active')
union
Select NULL LAST_NAME) t

2 Comments

Please note: "Top" is DBMS-specific and the OP didn't tag theirs, "union" is slower than "union all".
Without an ORDER BY clause the DBMS is free to pick any rows with TOP, LIMIT, FETCH FIRST ROW ONLY or whatever clause the DBMS features. So, even in case there is a match, you may still see NULL as the result. And yes, UNION [DISTINCT] makes no sense, because there are no duplicates to remove. Use UNION ALL instead. And yes again, TOP and the brackets ([]) are not standard SQL and only work in few DBMS.

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.