0

I want to search for non null values from the 'currentsheet' which works fine but some fields are actually blank rather than null. How can I find blank fields using postgreSQL as the below has not worked and still displays blank values under the 'currentsheet' field.

SELECT *
    FROM   PUBLIC._http_requests
    WHERE  (_http_requests.currentsheet IS NOT NULL OR _http_requests.currentsheet <> '')
           AND _http_requests.session_id IS NOT NULL
           AND _http_requests.http_referer IS NOT NULL

2 Answers 2

1

You need to use AND to check _http_requests.currentsheet. If it was NULL, then it would always be true for the <> '' check and vice versa.

As a way simpler example, you can use select statements without a table to help debug this sort of thing (from psql or whatever SQL query tool you like):

select (''   is not null or ''   <> '') as empty_result,
       (null is not null or null <> '') as null_result;

 empty_result | null_result 
--------------+-------------
 t            | 

If the string is '', you get true. If the string is null, you get null (this is because comparisons with null are SQL oddities -- select null = null; results in null). Let's see what happens when we replace or with and:

select (''   is not null and ''   <> '') as empty_result,
       (null is not null and null <> '') as null_result;

 empty_result | null_result 
--------------+-------------
 f            | f

Neat! With X is not null and X <> '', we get false when X is either '' or null.

So the way to phrase the select statement to do what you actually want is:

SELECT *
  FROM PUBLIC._http_requests
  WHERE _http_requests.currentsheet IS NOT NULL
    AND _http_requests.currentsheet <> ''
    AND _http_requests.session_id IS NOT NULL
    AND _http_requests.http_referer IS NOT NULL;
Sign up to request clarification or add additional context in comments.

Comments

0

I think you just need AND _http_requests.currentsheet <> ''. The AND is important there so that we exclude both.

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.