3

Let's assume i have a query

SELECT val FROM tbl

I want to write query that will output "ValueIsNotNull" when val is not null and "ValueIsNull" when val is null. Is it possible to do this with one line. As of now i found only ISNULL(val,'ValueIsNull') AS val1. But if value is not null then original value is printed.

3 Answers 3

9
select
    case
        when val is null then 'ValueIsNull'
        else 'ValueIsNotNull'
    end
    as NewVal
from tbl
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that works. And one offtopic question. Is there any way in C# to write multiple line strings?
Yes, use the @ in front of the string for multiline strings. That's a very random question.
3

You want the case statement function:

select (case when val is NULL then 'ValueIsNull' else 'ValueIsNotNull' end)

Comments

3
SELECT CASE WHEN val IS NULL THEN 'ValueIsNotNull' ELSE 'ValueIsNull' END

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.