user1640555533423
user1640555533423

Reputation: 45

How to search a row to see if any of the columns contain a certain string? In SQL

I have a table with 10 columns that each contain string values. I want to run a query that will return any of the rows which have any column value that matches a given string or set of strings.

Is there any way to do this?

The DBMS is MsSQL

Upvotes: 2

Views: 2964

Answers (1)

Shreyas Prakash
Shreyas Prakash

Reputation: 614

if you want exact match, you can use IN keyword and check in all columns

SELECT *
FROM tablename
WHERE 'your string' IN (column1, column2, )

if you want partial match then you have to use LIKE

SELECT *
FROM tablename
WHERE column1 LIKE '%your string%' or column2 LIKE '%your string%' ...

or you can add all columns and do one LIKE check

SELECT *
FROM tablename
WHERE CONCAT(column1,'#',column2,'#',column3,'#',...) LIKE '%your string%' 

Upvotes: 2

Related Questions