Rikky Saxena
Rikky Saxena

Reputation: 13

Using Where Clause with two type of conditions on same column

I do have a query like below and would like to include one further condition on this Select query.

I need to add a condition for Column7 , that it should choose value as 'FALSE' only when Column9 is not a pattern of certain values. say column9 is not like 'abcd%'.

Basically i want this query to not filter records on the condition of Column7 as FALSE when Column9 is of a certain pattern.

Query -

SELECT column1, column2, column3
FROM schema.table1 
WHERE
    column4 > TO_DATE('2018-02-02', 'yyyy-MM-dd')
    AND column5 IS NULL
    AND column6 IS NOT NULL
    AND Column7 = 'FALSE'
    AND Column8 <> '111111';

Upvotes: 0

Views: 85

Answers (3)

Nikhil
Nikhil

Reputation: 3950

you can also use REGEXP_LIKE for matchinig:-

SELECT column1, column2, column3
FROM schema.table1 
WHERE
    column4 > TO_DATE('2018-02-02', 'yyyy-MM-dd')
    AND column5 IS NULL
    AND column6 IS NOT NULL
    AND ( not REGEXP_LIKE (Column9, 'abcd[a-z]*', 'c') OR Column7 = 'FALSE')
    AND Column8 <> '111111';

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521053

Use an AND condition in your WHERE clause:

SELECT column1, column2, column3
FROM schema.table1 
WHERE
    column4 > TO_DATE('2018-02-02', 'yyyy-MM-dd') AND
    column5 IS NULL AND
    column6 IS NOT NULL AND
    (Column7 = 'TRUE' OR Column9 NOT LIKE 'abcd%') AND
    Column8 <> '111111' ;

Upvotes: 0

IShubh
IShubh

Reputation: 364

Try the follwing query-:

SELECT column1, column2, column3
FROM schema.table1 
WHERE
    column4 > TO_DATE('2018-02-02', 'yyyy-MM-dd')
    AND column5 IS NULL
    AND column6 IS NOT NULL
    AND (Column9 LIKE 'abcd%' OR Column7 = 'FALSE')
    AND Column8 <> '111111';

SQL Server

Upvotes: 3

Related Questions