Jori
Jori

Reputation: 1152

MySQL detect whether a field is empty or not

I have a large database consisting of multiple columns. I want to select one column that is of a small size and check whether another column that is of a very large size (measured in bytes) is null or empty. Of course I could just select them both and let the server-sided scripting language decide whether the second column is empty or not, but I really do not need the data of the second column, I only want to know if it is non null and non empty.

Now, since I'm not a MySQL ninja or anything, I am not sure if there is a possibility in MySQL to only select the "non-emptiness" of a column, and if this speeds things up when handling large columns. I ask this because I can imagine that MySQL handles these cases internally very differently, since there is no need for large amounts of disk reading, memory copying, piping to the server-sided script interpreter, etc. for the large second column, when only requesting a Boolean about its emptiness.

So

  1. is it possible to only select the non-emptiness of a column and
  2. does that speed up the query when that column is of significant size?

I do not want to filter by it, rather I just want to get a column containing an entry true/false, depending on whether the large column is non-empty, for every row.

Thank you very much in advance for taking the effort to read and answer this. If this has been answered before and I failed to find that answer, please let me know in the comments with a link and I'll remove this question immediately.

Upvotes: 1

Views: 54

Answers (1)

Mureinik
Mureinik

Reputation: 311143

You can do this in SQL:

SELECT small_column
FROM   my_table
WHERE  large_column IS NULL OR LENGTH(large_column) = 0

EDIT:
To answer the question in the comment, you can put expressions in the select list:

SELECT small_column, 
       (large_column IS NULL OR LENGTH(large_column) = 0) AS
           is_large_column_empty
FROM   my_table

Upvotes: 2

Related Questions