5

To query empty fields I have seen this answer: Postgresql, select empty fields

(unfortunately I don't have enough reputation points to answer @wildplasser on that post, so here we go)

Wildplasser's answer:

SELECT mystr, mystr1 
FROM mytable 
WHERE COALESCE(mystr, '') = '' 
   OR COALESCE(mystr1, '') = ''
    ;

I am not sure I get the COALESCE method, but it also works for me this way (specific for my string data type):

SELECT mystr, mystr1 
FROM mytable 
WHERE mystr = '' ;

My questions are:

  1. Does COALESCE work for any data type?

  2. Is there any better way to query empty strings? i.e., column_value = ' '

3 Answers 3

4

First you need to understand the difference between NULL and "empty".

NULL is the absence of a value. Any (or at least almost any) data type can be NULL. When you have a column of type integer, and you don't want to put a value in that field, you put NULL.

"Empty" is a string/text concept. It's a string with an empty value, i.e. ''. A text field with an empty string contains a value: the empty string. It is not the same as containing NULL, i.e. no value. Other data types e.g. integer, boolean, json, whatever, can't have an empty string.

Now to COALESCE. That function works on any data type, and basically it returns the first not-NULL result of its arguments. So COALESCE(NULL, TRUE) returns TRUE because the first argument is NULL; COALESCE(FALSE, TRUE) returns FALSE because the first argument is not NULL; and COALESCE(NULL, NULL) returns NULL because there are no not-NULL arguments.

So, COALESCE(field, '') returns the value of field if it's not NULL, and otherwise returns an empty string. When used in COALESCE(field, '') = '' when trying to find any rows where field is "empty", this is basically saying "if field is NULL then use an empty string in its place, then see if it equals an empty string". This is because NULL and an empty string are not equivalent, and "you" are trying to find any rows where fields are NULL or empty.

In your version of the query, where you just do field = '', that will ONLY return results where field is actually an empty string, not where field is NULL. Which behaviour you desire is up to you.

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

1 Comment

COALESCE(NULL, 'Brilliant description of COALESCE thanks')
2

With COALESCE you will get NULL values too in the first query.

1- In Postgresql, you can't mix datatype example here, but you can use the function to_char to mix values

2- I don't understand your question

3 Comments

I mean if my way of doing it would be efficient for millions of rows, i.e, Is there any better way to query empty strings? i.e., column_value = ' ' ... since I am not an expert in postgresql I am always concerned about creating tables or querying them in an inefficient way
What do you want to do for Null Values ?
nothing really, theoretically in my query I will always set/update values with empty or none empty strings
1
  1. I think based on the definition of coalesce itself as "The COALESCE() function returns the first non-null value in a list." means that it work for any data type

  2. I don't really understand the question but i think yes its already the most efficient way to make empty string

1 Comment

This has already been answered.

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.