MANNADIYAR
MANNADIYAR

Reputation: 25

Using 'LIKE' inside IF statement

I need to sort a table using a condition. I used the below statement,

select * from table1 order by if (col1 like '%Cochin%', substr(col1,1,4), col1)

But I got an error, "missing right parenthesis".

Upvotes: 1

Views: 16687

Answers (2)

PAVITRA
PAVITRA

Reputation: 861

If you are using Oracle,
Try this :

    IF NOT REGEXP_LIKE(string, pattern) THEN
    .... 
    END IF;

Upvotes: 0

RustProof Labs
RustProof Labs

Reputation: 1277

Use a CASE statement like this:

SELECT * 
    FROM table1 
    ORDER BY CASE WHEN col1 LIKE '%Cochin%' THEN SUBSTR(col1,1,4)
        ELSE col1 END

Upvotes: 1

Related Questions