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?

5
  • 2
    Look up the NOT clause and LIKE clause in SQL. Commented Sep 4, 2021 at 16:55
  • Does this answer your question? SQL SELECT WHERE field contains words Commented Sep 4, 2021 at 16:56
  • I am looking for a function which can be used in a case statement Commented Sep 4, 2021 at 17:10
  • stackoverflow.com/q/6740134 Commented Sep 4, 2021 at 17:11
  • Why do you want to use a function when a simple SQL command solves the problem? Commented Sep 4, 2021 at 18:30

2 Answers 2

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%');
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.