HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Find rows where any column matches a search pattern

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
rowsmatchessearchcolumnanywherefindpattern

Problem

I have a table "Test" with 15 different columns of different data types.

I'm looking for rows having characters like "ABC". But "ABC" can be present in any column and I want to find whole records having "ABC".

I cannot restrict the row using a WHERE clause, because I really do not have any idea in which column the ABC resides? Is there a way to do this?

Solution

Well, there is the plain and simple "brute force" way:

SELECT *
FROM   "Test"
WHERE (col1 LIKE '%ABC%' OR
       col2 LIKE '%ABC%' OR
       col3 LIKE '%ABC%' OR
       ...
       col15 LIKE '%ABC%');


Parentheses won't be necessary if you don't have additional WHERE expressions.
Probably faster (and more robust in any case) than concatenating all columns for a single LIKE test.

Code Snippets

SELECT *
FROM   "Test"
WHERE (col1 LIKE '%ABC%' OR
       col2 LIKE '%ABC%' OR
       col3 LIKE '%ABC%' OR
       ...
       col15 LIKE '%ABC%');

Context

StackExchange Database Administrators Q#50574, answer score: 5

Revisions (0)

No revisions yet.