Skip to content
Advertisement

How to check if SQLite row is empty

I have the following which only inserts to column two if the value is empty:

UPDATE name 
SET one=?,
    two=COALESCE(two, ?),
    three=? 
WHERE id = ?

However, this only works if column two if the value is null. How can I get this to work if it is either NULL OR an empty string.

It should always insert to one and three. It should only insert to two if it is NULL or empty

I am using SQLite with PHP

Advertisement

Answer

If by “empty” you mean an empty string, you can use nullif():

two = COALESCE(NULLIF(two, ''), ?)

The more general solution is a CASE expression.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement