SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Wildcard Basics Recap – Day 1 of 35

Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 1.
Every day one winner from United States will get Joes 2 Pros Volume 1.

Wildcard ranges

If you have ever been to a convention where they have a morning registration desk that must handle thousands of people in a short time you know they must put some pre-planning thought into how to handle this burst of volume. In fact often they will have many registration desks running in parallel to make things run faster. The first registration desk might handle all customer last names starting from A to K. Desk 2 will handle names from L to Q and the third desk will handle from R to Z.

With my last name being Morelan I would naturally head to registration desk #2 knowing that desk has the list with my name on it and the other desks don’t. Now let’s say you are in charge of creating these three separate lists and sending them out to the right registration workers. You know how to sort but how can you separate this lists using wildcards?

Wildcard Basics Recap

Lets start off with something most of us know already. Most SQL folks understand the usefulness and power of the basic uses of wildcards. Using wildcards allows you to do pattern matches in a column. In this case our criteria does not want to use the = sign to find a pattern match. The operator that allows you to do approximate predicates is LIKE. The LIKE operator allows you to do special relative searches to filter your result set.

--Find all LastNames that start with the letter A
SELECT *
FROM Employee
WHERE LastName LIKE 'A%'

To find everyone whose last name starts with the letter B, you need “B” to be the first letter. After the letter B you can have any number of characters. Using B% in single quotes after the LIKE operator gets all last names starting with the letter B.

--Find all LastNames that start with the letter B
SELECT *
FROM Employee
WHERE LastName LIKE 'B%'

Wildcard ranges or set specifiers

If you want to find all LastName values starting with the letters A or B you can use two predicates in your WHERE clause. You need to separate them with the OR operator.

--Find all LastNames that start with the letter B
SELECT *
FROM Employee
WHERE LastName LIKE 'A%'
OR LastName LIKE 'B%'

Finding names beginning with A or B is easy. How about the registration desk example where want the names ranging from A-K? This works well until you want a range of A-K as in the example below:

--Find all LastNames ranging from A-K
SELECT *
FROM Employee
WHERE LastName LIKE 'A%'
OR LastName LIKE 'B%'
OR LastName LIKE 'C%'
OR LastName LIKE 'D%'
OR LastName LIKE 'E%'
OR LastName LIKE 'F%'
OR LastName LIKE 'G%'
OR LastName LIKE 'H%'
OR LastName LIKE 'I%'
OR LastName LIKE 'J%'
OR LastName LIKE 'K%'

The previous query does find LastName values starting from A-K. However, if you need a range of letters, the LIKE operator has many better options. We only really care about the first letter of the last name and there a several first letters that fit with what were looking for. The first letter of the last name can be A,B,C,D,E,F,G,H,I,J or K. Simply list all the choices you want for the first letter inside a set of square brackets.

--LastNames ranging from A to K using a set of 11 letters
SELECT *
FROM Employee
WHERE LastName LIKE '[ABCDEFGHIJK]%'

Square brackets with wildcards enclose ranges or sets for 1 position. In this case the first position is a set of 11 different possible letters. This is not a series of letter but a multiple choice of letters. For example this works regardless of the order you put your letters in. This code sample below does the exact same thing.

--LastNames ranging from A to K using a set of 11 letters
SELECT *
FROM Employee
WHERE LastName LIKE '[KBCDEFGHIJA]%'

Again the set is how many letters you put in the square brackets. The code below is a logical mistake where you won’t get A to K but you just get A or K for the first letter.

--Find all LastNames starting with A or K (Mistake
SELECT *
FROM Employee
WHERE LastName LIKE '[AK]%'

Since we’re looking for the first letter to be within a range  from A to K, we specify that range in square brackets. This is even easier than using a set. The wildcard after the brackets allows any number of characters after the range.

--LastNames ranging from A to K using a range
SELECT *
FROM Employee
WHERE LastName LIKE '[A-K]%'

Note: this range will not work if your LIKE was changed to an equal (=) sign. The following code will not return any records to your result set:

--Bad query (it won’t error but returns no records)
SELECT *
FROM Employee
WHERE LastName = '[A-K]%'

Note: If you want to setup the sample JProCo database on your system you can watch this video.

Question 1

Q. 1) You want to find all first names that start with the letters A-M in your Customer table. Which SQL code would you use?

  1. SELECT * FROM Customer
    WHERE Firstname <= ‘m%’
  2. SELECT * FROM Customer
    WHERE Firstname  = ‘a-m%’
  3. SELECT * FROM Customer
    WHERE Firstname like ‘a-m%’
  4. SELECT * FROM Customer
    WHERE Firstname  = ‘[a-m]% ‘
  5. SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’

Please post your answer in comment section to win Joes 2 Pros books.

Rules:

Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 1.
Winner from India will get Joes 2 Pros Volume 1.
The contest is open till next blog post shows up at which is next day GTM+2.5.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Scripts
Previous Post
SQL SERVER – Win a Book a Day – Contest Rules – Day 0 of 35
Next Post
SQL SERVER – Tips from the SQL Development Series – Wildcard – Querying Special Characters – Day 2 of 35

Related Posts

334 Comments. Leave new

  • 5.SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’

    Reply
  • regarding Option 1:

    Can we have more details on how the SQL Engine treats the <= Operator with text expression? Will it try to use the ASCII value?

    Reply
  • 5. SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’

    Uses a specified range, in this case A through M, for the first letter (wildcard suffix) from the FirstName column.

    USA

    Reply
  • Answer: 5 –>

    SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’

    country: USA

    Reply
  • Correct answer is option 5.

    Cheers,
    Jason
    From USA

    Reply
  • Correct answer is #5 and it worked like this:

    SELECT UserName, UserId
    FROM ASPNETDB.dbo.aspnet_Users
    WHERE UserName Like ‘[a-k]%’

    USA

    Reply
  • Answer is Number 5

    Reply
  • A.Arul Prakash
    August 2, 2011 1:52 am

    Pinal,

    Option : 5 is correct.

    Option:1 -> wrong. We need only name which starts A-M but this query will fetch even if there are any name which starts with special characters (*,-)
    Option:2 -> Wrong. We should not use “=” operator
    Option:3 -> Wrong. This query will exactly fetch only when the name start with ‘a-m’ (example if the first name is “a-master”)
    Option:4 -> Wrong. We should not use “=” operator
    Option:5 -> Correct. It will fetch all the names which start with the character between A to M

    Country : USA (Chicago)

    Reply
  • dilipkumarjena
    August 2, 2011 1:54 am

    Option 1: This option will give values till M. That is starting from A to M and after that it truncates the name which has length more than one letter.
    ex : we will get till M but not M. (or) Matt (or) Ma etc so this option Fails even if we specify
    SELECT * FROM Customer WHERE Firstname <= ‘n%’ this will satisfy our required condition but will also give extra records which has First name starting to correct 1 place i.e. N but not n. (or) Naa etc so this option is wrong.
    Yes we cannot apply expression on to letters or words but here its taking ASCII values and matching there by.

    Here, Empty and empty space will be selected because by default it comes from the start but 'NULL' values will not come here.One more reason why this option is wrong.

    Option 2: and Option 3: returns Nothing so wrong answer.

    Option 4:Bad query (it won’t error but returns no records) as explained by you wrong answer.

    Option 5: The LIKE operator looks for words with those letters in the range. The brackets will show those range of names that begin with those letters. The wildcard after the brackets within the single quotes will search for every name that begins with the letters in the brackets.
    Here no nulls and no empty results turns up so the Right option is Option 5:

    Many thanks for helping all of us work together.
    i am looking forward to many more like this.

    Country: India

    Reply
  • thesistesting
    August 2, 2011 2:53 am

    5. SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’

    Reply
  • the 5th option work fine. so my ans is 5th one

    SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’

    Reply
  • The answer is #5
    SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’

    Thanks,
    Tricia (USA)

    Reply
  • DiveSh Singhvi
    August 2, 2011 3:00 am

    5. SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’

    Reply
  • Should you decide to include Australia in your prize pool :)

    The correct answer is #5.

    Option 1 is almost correct. It will find everybody who’s name starts with A-L, however the <= is restricting the name to be less that M… so M[arty] for example will not be returned. This could be modified to < 'n%' to get a correct result.

    Option 2 is incorrect, it is looking for a string literal a-m.

    Option 3 is also incorrect as it is also looking for a string literal a-m

    Option 4 is incorrect as it will return rows matching [a-m]% exactly not those like it

    Reply
  • The right answer is

    #5 – SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’

    Because like in Regular Expressions the metacharacters [] means a class of characters to be searched, in this case a class of character range from a through m [a-m] mixed with the wildcard character %, so the whole expression can be explained like:

    Find all rows in the customers table where the values in the Firstname field begins with any character from a through m.

    That´s the way i understand the #5 is tye right answer.

    Country: México

    Reply
  • Answer is
    5. SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’
    All other options doesnt use “like” notation, which will not recognise the special string format.

    Country: Australia

    Reply
  • SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’
    order by customer

    – Melbourne, Australia

    Reply
  • Answer is Option 5
    Country: USA
    Thanks

    Reply
  • Answer is

    5.SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’

    Country : India[Ahmedabad]
    Thank you

    Reply
  • Uday Kumar B R
    August 2, 2011 9:22 am

    Question 1

    Q. 1) You want to find all first names that start with the letters A-M in your Customer table. Which SQL code would you use?

    Answer is
    5. SELECT * FROM Customer
    WHERE Firstname like ‘[a-m]%’

    Reply

Leave a Reply