Link Search Menu Expand Document

Module 2: How to Filter Data with WHERE Clause - Part 2

Previous Tutorial - Next Tutorial

Let’s start with another operator which is the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specific string pattern in a column. So the percentage and underscore are the two wildcard characters using which you can search for a specific string pattern in conjunction with the like operator.

So LIKE operator is used to finding those records that will start with some particular string or end with some particular string or contain some particular string. Now in this person table, I want to find that person whose last name starts with the Walt word. So for that, I need to write the WHERE clause something like (WHERE LastName LIKE 'walt%') So now if I execute this query, I will get that person whose last name started with the Walt for example Walters, Walton, etc?

Now I want to find that person whose first name ends with the word VIN and I need to write WHERE clause something like (WHERE LastName LIKE '%vin'). Now if I execute this query, I will get the person whose last name ends with a VIN word. So for example Devin, Kevin. Kelvin etc.

So in LIKE operator to find the start with character string you need to specify this wildcard character in the end and to find those records that end with some particular character string. So for that, you need to define this wildcard character in the starting. Now in the next requirement, I want to find that person whose last name contains the word 'REN'. So the REN word occurs anywhere in the name so it can occur in the starting, It can occur at the end of it may be present in the middle of this name.

So let's see how we can use the LIKE operator to find the records for that last name. So if I execute this query I will get the person whose last name contains REN. So it can be anywhere. It can be in the middle. It can be in the starting and it can be in the End.

This is straightforward but sometimes you are in a situation where you want to find wildcard characters like percentage and underscored from the column value itself. So to find these wildcard characters from the column value is not very straightforward which you might think but SQL server provides a way to search for wildcard characters.

How to Filter PDF Data

We can search for it using the escape option. Let's see how we can use the ESCAPE option. So to demonstrate that ESCAPE option I need to use another table which is a product photo table. So let me open a new query window. Now in this query, I want to search for those rows in which this large photo file name column has the character green underscore but not is that underscored is a wildcard character.

Without spacing the escape option the query would search for any description value that contains the word green followed by any single character other than the underscore. So if I execute this query, we will get the occurrence of green as well as any single character in this record. So now to skip these two (result sl 2 & 6) records I need to redefine this query and use the ESCAPE option. Let's see how we can use the ESCAPE option (WHERE LargePhotoFileName LIKE ‘%green!_%’ ESCAPE ‘!’) and execute this query. Two records are now discarded.

Now this time I'm getting only those records whose value contains a green underscore. Now let's move on to the next operator which is IN operator. Now the IN operator allows you to specify multiple values in a WHERE clause. You can see that IN operator is a shorthand for multiple or conditions.

Let's see the IN operator in action. Now in this query, I want to find that person whose title is either Senior or Mrs. Let's see how we can use the IN operator to find those records. So for that, I need to write WHERE clause, and when I execute the query I will get only those people whose title is either Senior or Mrs.

How to Filter Data PDF

Now, this query can be rewritten using the OR operator. Now when I execute both queries then I will get the same record. So similarly you can use this not IN operator in this query. For example, If I write not an operator and execute this query then I will get that person whose title not is either Senior or Mrs.

Web API for developers Free Trial Offline SDK

Here's SQL video tutorial:

Other useful tutorials:


Back to top

© , Learn SQL 24 / 7 — All Rights Reserved - Terms of Use - Privacy Policy