0

I am constructing a SQL Developer query to retrieve records with a specified status'. Multiple values can also be selected. How can I update my query to include records with multiple "Status" values? which are semicolon delimited i.e. Off; Pending

Here is my query which only returns records with a singular status:

Select * from LOGS where Status= 'Off' or Status = 'Pending' or Status = 'ON'; 
3
  • 1
    How can status be 'Off' AND 'Pending' AND 'On'? Commented Jun 2, 2017 at 20:07
  • Please add sample data and the desired result. Commented Jun 2, 2017 at 20:08
  • What do you mean - do you have a column STATUS and the values can be 'Off' or 'Pending' but they can also be 'Off;Pending'? (Or, with a space after the semicolon?) If so, why can't you add or Status = 'Off; Pending'? So perhaps that is not what you mean? Please clarify. Commented Jun 2, 2017 at 20:12

2 Answers 2

1
Select * from LOGS where Status= 'Off' and Status = 'Pending' and Status = 'ON'; 

you must mean OR´s instead of AND´s, the same column cannot have those 3 values on the same row

   Select * from LOGS where Status= 'Off' or Status = 'Pending' or Status = 'ON'; 

this change will allow you to get every LOGS row where any of those 3 status is valid

if you mean that status can have multiple values like "Off; Pending;ON" all you have to do is:

 Select * from LOGS where Status like '%Off%' or Status like '%Pending%' or Status like '%ON%'; 

this means "bring every status that have Off on it, even if it is Offline, 123Off123" and so on to the other cases, so be carefull on your status

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

2 Comments

how do I now handle a case of multiple selected values?
is your question answered? i´m confused. can you be more specific?
0

How about:

Select * from LOGS
where
    concat('; ', Status,'; ') like '%; Off; %' or
    concat('; ', Status,'; ') like '%; Pending; %' or 
    concat('; ', Status,'; ') like '%; On; %'    ;

or perhaps better

Select * from LOGS
where
    REGEXP_LIKE (concat('; ', Status,'; '),'; Off; |; Pending; |; On; ')

5 Comments

'%Pending%' will not accept 'End', its case sensitive
Will that not depend on collation? I'm not so familiar with Oracle (thinking of SQL-Server)
i was answering for the sql case, i can´t test it now but i´m convinced it will not accept. if it does, i´m glad to hear. your solution with REGEX seems good but complex.
found something else, in your first answer you have: "; Off; ", it means that space characters are obligatory aswell, what if it is the last STATUS? will it have character ";" and " "?
@GabriT. I added semi-colons and spaces in my concat, and according to the OP, there is a space in when the field contains multiple items. It can be changed if necessary.

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.