Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

SQL condition LIKE

4 July 2020

SQL condition LIKE

The SQL condition LIKE allows you to use wildcards to match the pattern in the query. The LIKE condition is used in the WHERE clause of the SELECT, INSERT, UPDATE or DELETE operator.

Syntax for LIKE condition in SQL

expression_id LIKE pattern_id [ ESCAPE 'escapecharacter_id' ]

where:

  • expression_id – Symbolic expression such as a field or column
  • pattern_id – Symbolic expression that contains a comparison with the template. Substitution characters that you can select:
  • ESCAPE ‘escapecharacter_id’ – It’s optional. This allows you to match templates with literal wildcards such as % or _.

Using % wildcard in LIKE condition

Let’s look at how the % wildcard works in the LIKE SQL condition. Remember that the % wildcard matches any string of any length (including zero length).

In this first example we want to find all entries in the customers table where the l_name of the client starts with ‘C’.

In this example we have a customer table with the following data:

 
SymbolExplanation
%Allows comparing any string of any length (including zero length)
_Allows matching a single character

 

custom_idf_namel_namefav_website
4000JustinBiebergoogle.com
5000SelenaGomezbing.com
6000 MilaKunisyahoo.com
7000TomCruiseoracle.com
8000JohnnyDeppNULL
9000RussellCrowegoogle.com

 

Enter the following SQL statement.

SELECT *
FROM customs
WHERE l_name LIKE 'C%'
ORDER BY l_name;

Two entries will be selected. Here are the results that you should get.

custom_idf_namel_namefav_website
7000TomCruiseoracle.com
9000RussellCrowegoogle.com

 

In this example, records of the customers table are returned where last_name starts with ‘C’. As you can see, records by Cruise and Crowe names have been returned.
Since the LIKE condition is case insensitive, the next SQL statement will return the same results.

SELECT *
FROM customs
WHERE l_name LIKE 'c%'
ORDER BY l_name;

Using multiple % wildcards in LIKE condition

You can also use the % wildcard multiple times with the LIKE condition. Using the same table with the following data:

custom_idf_namel_namefav_website
4000JustinBiebergoogle.com
5000SelenaGomezbing.com
6000 MilaKunisyahoo.com
7000TomCruiseoracle.com
8000JohnnyDeppNULL
9000RussellCrowegoogle.com

 

Let’s try to find all the last_name values from the customers table where last_name contains the letter ‘e’.

Enter the following SQL statement:

SELECT l_name
FROM customs
WHERE l_name LIKE '%e%'
ORDER BY l_name;

Three entries will be selected. Here are the results that you should get.

l_name
Bieber
Gomez
Depp

 

In this example, the names Bieber, Gomez and Depp contain the letter ‘e’.

Using the wildcard _ in the condition LIKE

Next, let’s look at how the _ (underscore character) substitution works in the LIKE condition. Remember that the wildcard _ looks for exactly one character, unlike the % wildcard.

Using the table with the following data:

cat_idcat_name
25Deli
50Produce
75Bakery
100General Merchandise
125Technology

 

Let’s try to find all the records from the category table, where cat_id is 2 digits long and ends in ‘5’. Enter the following SQL statement.

SELECT *
FROM cats
WHERE cat_id LIKE '_5';

Two records will be selected. Here are the results you should get.

cat_idcat_name
25Deli
75Bakery

 

In this example, there are 2 entries that will match the template – cat_id with values 25 and 75. Note that cat_id equal to 125 was not selected because the symbol _ matches only one character.

Using multiple wildcards _ in LIKE condition

If you want to match a three-digit value ending in “5”, you will need to use the _ double wildcard. You can modify your query as follows.

SELECT *
FROM cats
WHERE cat_id LIKE '__5';

Now you will return cat_id value equal to 125.

cat_idcat_name
125Technology

Using NOT operator with LIKE condition

Next, let’s look at an example of using the NOT operator with the LIKE condition. In this example, we have a table with the following data:

suppl_idsuppl_namecity_idstate_id
100YandexMoscowRussian
200GoogleLansingMichigan
300OracleRedwood CityCalifornia
400BingRedmondWashington
500YahooSunnyvaleWashington
600DuckDuckGoPaoliPennsylvania
700QwantParisFrance
800FacebookMenlo ParkCalifornia
900Electronic ArtsSan FranciscoCalifornia

 

Let’s take a look at all the entries in the suppliers table where supplier_name does not contain literal ‘o’. Enter the following SQL statement.

SELECT *
FROM suppls
WHERE suppl_name NOT LIKE '%o%';

4 entries will be selected. Here are the results that you should get.

suppl_idsuppl_namecity_idstate_id
100YandexMoscowRussian
300OracleRedwood CityCalifornia
400BingRedmondWashington
700QwantParisFrance

 

In this example, there are four entries in the suppliers table where suppl_name does not contain literal ‘o’.

Using escape-symbols with LIKE condition

It is important to understand how to use the “escape-symbol” when it matches the template. You can screen % or _ and search for literal versions.

Let’s say you wanted to find % as a literal in the LIKE condition. You can do this with the escape-symbol. In our example we will use ! as an escape-symbol in the LIKE condition.

NOTE: You can only define an escape-character as one character. It is best to choose a character that will not appear in your data very often, for example ! or #.

In this example, we have a test table with the following data:

test1_idtest1_value
110%
225%
3100
499

 

We could return all entries from the test table where test1_value contains literal %. Enter the following SQL statement.

SELECT *
FROM test1
WHERE test1_value LIKE '%!%%' escape_id '!

Here are the results you should get.

test1_idtest1_value
110%
225%

 

In this example, the ! character is defined as an escape-symbol. The first and last % values in the LIKE condition are treated as normal wildcards. !% is shielded by %, so it is treated as a literal value of %.

You can additionally change the above example and return only those test_values that start with 1 and contain a literal %. Enter the following SQL statement:

SELECT *
FROM test
WHERE test_value LIKE '1%!%%' escape '!

Here are the results you should get.

test1_idtest1_value
110%

 

This example, this time, will only return one record. Because there is only one test_value that starts with 1 and contains literal %.

How to use the SQL LIKE Condition

 
Tags: , , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...