2

Can we use "Contains" and "NotContains" methods in SQL i.e., How do I check if a string or a column value is contained in another column of a table.

Problem Statement:

  1. How do I check if a string "ABC" is contained/Not contained in "Column1"
  2. How do I check if a "Column1" value is contained/Not contained in "Column2"

If not SQL can we use JavaScript UDF or any other means to make this happen?

ADom
  • 151
  • 1
  • 9

2 Answers2

1

CONTAINS:

Returns true if expr1 contains expr2. Both expressions must be text or binary expressions.

CONTAINS( <expr1> , <expr2> )

Examples:

SELECT * FROM tab WHERE (Colum1, 'ABC');
SELECT * FROM tab WHERE NOT (Colum1, 'ABC');

SELECT * FROM tab WHERE (Colum1, Column2);
SELECT * FROM tab WHERE NOT (Colum1, Column2);

It is worth mentioning Snowflake LIKE/ILIKE [ANY]:

SELECT *
FROM tab
WHERE Column1 LIKE ANY ('%ABC%','%XYZ%');
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
0

You can use Like operator I believe i am not sure what exactly are you looking for but here is an example below. For more details: https://www.w3schools.com/sql/sql_like.asp

select column
from [table]
where column Like %ABC%

or if you already know the values that you are looking for in a specific column then you can use in operator:

select column from [table] where column in ('ABC','XYZ')

This will give you the rows for the column, where the values are equal to either ABC or XYZ

trillion
  • 1,207
  • 1
  • 5
  • 15