Skip to content
Advertisement

sqlite select hard coded text given a matching “or” condition

I have a table like this:

key,c1,c2,c3,...
--------------
key1,v1,v2,v3, ...
.....
keyn,vn,,,,,...

I am querying for column values whose length is 0 using a statement like this:

SELECT key FROM table WHERE length(c1) = 0 OR length(c2) = 0 OR ... OR length(cn) = 0;

I’d like to have a text associated with the key indicating which condition lead to the match. so something like

SELECT key,"column 1 length is zero" OR "column 2 length is zero"  ... OR "column N length is zero" FROM table WHERE length(c1) = 0 or length(c2) = 0 or ... or length(cn) = 0;

not sure how to do this

Advertisement

Answer

You can use a CASE expression for this. On the other hand, note that in SQLite you can use MIN() to simplify the WHERE clause:

select 
    key,
    case 
        when length(c1) = 0 then 'column 1'
        when length(c2) = 0 then 'column 2'
        when length(c3) = 0 then 'column 3'
    end || ' is length 0' which
from mytable
where min(length(c1), length(c2), length(c3)) = 0
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement