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

Time:10-13

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

CodePudding user response:

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%' 

  •  Tags:  
  • sql
  • Related