4

I've written a very simple query

select *, "Status" from "ABC" WHERE "ABCId" = 7; // Got 100 rows

ABC table has some columns one of them is "Status" with null value usually appearing as (Null)

When I try these queries

-- 1
select * FROM "ABC" where "ABCId" = 7 AND "Status" <> 'success'

-- 2
select * FROM "ABC" where "ABCId" = 7 AND "Status" != 'success'

-- 3
select * FROM "ABC" where "ABCId" = 7 AND "Status" NOT ILIKE '%success%'

I'm not getting any rows, I bang my head, this is a simple query :/

6
  • 2
    AND status IS NULL Commented Jul 28, 2019 at 6:51
  • No some times it might have the value 'failed' in that case, I should consider null & failed rows too Commented Jul 28, 2019 at 6:57
  • Then use an OR: AND status IS NULL OR status <> 'success' Commented Jul 28, 2019 at 6:58
  • what's the wrong with my query it is the basically same right? Commented Jul 28, 2019 at 7:01
  • 1
    Well, doesn't your experiment show you what the difference is? null is neither = nor <> to anything. tutorialspoint.com/sql/sql-null-values Commented Jul 28, 2019 at 7:03

2 Answers 2

13

You can use the null safe comparison operator is distinct from:

and "Status" is distinct from 'success'

that will be true for null values as well.

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

1 Comment

Working, Alternative -> select * FROM "ABC" where "ABCId" = 7 AND COALESCE("Status", '') <> 'success'
0

Give coalesce a try

select "ABCId", coalesce(status, 'unknown') from "ABC" where "ABCId" = 7 and ("Status" != 'success' OR status IS NULL);```

1 Comment

@Arjun what number of rows do you get when you try ABCId=7 and Status='success' ? also what number for when you try ABCId=7 and Status='failure' ?

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.