SQL Not Like with Multiple Values - SQL Training Online (2024)

by Joey Blue 1 Comment

I came across a forum post where someone wanted to use SQL NOT LIKE with multiple values.

They were trying to exclude multiple values from the SQL query, but they were needing to use wildcards.

If you wanted to just filter values without wildcards, you would use the following query.

select *from table1where column1 not in ('value1','value2','value3');

The only problem was that they needed to compare using the LIKE operator.

It would be nice if you could just stick some wildcards in the in clause like this:

where column1 not in ('%value1%','%value2%','%value3%')

But, you can’t stick a wildcard inside of the IN clause. So, here is the easiest solution.

select *from table1where column1 not like '%value1%'and column1 not like '%value2%'and column1 not like'%value3%';

If you want to play around with the Boolean logic, yourearrangethe query like this.

select *from table1where not (column1 like '%value1%'or column1 like '%value2%'or column1 like'%value3%');

SQL Not Like with Multiple Values - SQL Training Online (1)

What happens if you don’t put those parenthesis in?

Here are a few other posts you might enjoy:

SQL Wildcards | SQL Multiple Filter |How to use the SQL In Statement with Subquery

You can visit me at any of the following:

SQL Training Online:https://www.sqltrainingonline.com

Twitter:http://www.twitter.com/sql_by_joey

Google+:https://plus.google.com/#100925239624117719658/posts

LinkedIn:http://www.linkedin.com/in/joeyblue

Facebook:http://www.facebook.com/sqltrainingonline

Related

SQL Not Like with Multiple Values - SQL Training Online (2024)

FAQs

Can you do a like with multiple values in SQL? ›

The Power of SQL LIKE for Multiple Values

In the realm of SQL, the capability to handle multiple values efficiently is a game-changer. SQL offers a powerful feature that combines the LIKE operator with the IN keyword, elevating search functionalities to new heights.

How do I not include multiple values in SQL? ›

To exclude multiple records in SQL, you can use the `NOT IN` clause with a list of values you want to exclude. This clause is often used in the `WHERE` statement to filter out specific rows based on one or more conditions.

How to do a not like query in SQL? ›

SQL not like statement syntax will be like below. SELECT column FROM table_name WHERE column NOT LIKE pattern; UPDATE table_name SET column=value WHERE column NOT LIKE pattern; DELETE FROM table_name WHERE column NOT LIKE pattern; As an example, let's say we want the list of customer names that don't start with 'A'.

How do you handle multiple values in SQL? ›

If you want to insert many rows into a SQL table, you have to repeat INSERT INTO over and over in separate statements. INSERT INTO Person VALUES (1, "Amir"); INSERT INTO Person VALUES (2, "Sofia"); INSERT INTO Person VALUES (3, "Aya"); ...

What is like %% in SQL? ›

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign % represents zero, one, or multiple characters.

What is the difference between like and similar to in SQL? ›

The SIMILAR TO operator returns true or false depending on whether its pattern matches the given string. It is similar to LIKE , except that it interprets the pattern using the SQL standard's definition of a regular expression.

Does not equal multiple values in SQL? ›

Exploring SQL NOT EQUAL Operator

The NOT EQUAL operator ( <> or != ) is indispensable for querying data that does not meet a certain condition. It enables the exclusion of specific rows from the results, making data analysis more precise and relevant.

What does <> do in SQL? ›

We use SQL Not Equal comparison operator (<>) to compare two expressions. For example, 10<>11 comparison operation uses SQL Not Equal operator (<>) between two expressions 10 and 11.

How do I replace multiple values in SQL query? ›

Its basic syntax is:
  1. REPLACE(text, search_string, replace_string)
  2. SELECT REPLACE('MySQL', 'M', 'W');
  3. SELECT REPLACE(REPLACE('MySQL', 'M', 'W'), 'S', 'Z');
Jan 4, 2024

Can we use not with like in SQL? ›

SQL NOT LIKE Operator

We can also invert the working of the LIKE operator by using the NOT operator with it.

What can I use instead of SQL not in? ›

  • Alternatives to SQL NOT IN.
  • Using NOT EXISTS.
  • LEFT JOIN / IS NULL.
Feb 7, 2024

What is like opposite in SQL query? ›

MySQL NOT LIKE Syntax

The MySQL NOT LIKE operator works in the opposite way to the LIKE comparison operator. Instead of retrieving data according to the specified pattern, it finds everything that does not match it. This operator can be useful when we want to exclude specific data from the search results.

How do I exclude multiple values in SQL query? ›

To exclude multiple values to be fetched from a table we can use multiple OR statements but when we want to exclude a lot of values it becomes lengthy to write multiple AND statements, To avoid this we can use the NOT IN clause with the array of values that need to be excluded with the WHERE statement.

How to query multiple like values in SQL? ›

Syntax: SELECT col1, col2, col3... colN WHERE (column_name LIKE 'pattern' OR column name LIKE 'pattern' OR column name LIKE 'pattern') FROM TABLE_NAME; In a SQL query, many like statements are possible.

How do I SELECT multiple values from multiple tables in SQL? ›

Example syntax to select from multiple tables:
  1. SELECT p. p_id, p.cus_id, p.p_name, c1.name1, c2.name2.
  2. FROM product AS p.
  3. LEFT JOIN customer1 AS c1.
  4. ON p.cus_id=c1.cus_id.
  5. LEFT JOIN customer2 AS c2.
  6. ON p.cus_id = c2.cus_id.

Can you combine in and like SQL? ›

SQL Combine 'LIKE' and 'IN' Operator

By combining LIKE and IN, users can filter results based on patterns and specific values simultaneously, allowing users to create more complex and precise search conditions in SQL statements.

How do I combine multiple values in SQL? ›

To combine values from multiple columns into a single column in a query, you can use the CONCAT function. In some databases, such as DB2, Oracle, and PostgreSQL, you can use the double pipe operator (||) as a shortcut for the CONCAT function. In SQL Server, you can use the plus sign (+) as a shortcut for CONCAT.

How do I compare multiple values in SQL query? ›

How to compare values in multiple columns in SQL Server. To compare values stored in several SQL columns, you can use the CASE statement, which returns a result based on the comparison. The CASE statement includes at least one pair of WHEN and THEN statements. The WHEN clause defines the condition to be checked.

Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 5754

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.