Quiz: ESCAPING LIKE in TSQL

Before we begin…

The code samples in this quiz use a table value constructor to make the code samples easily portable. The table value constructor makes a 5 row table that looks like this:

TRUTH

Quiz: ESCAPING LIKE in TSQL

The only difference in each query is in the WHERE clause
  • SELECT *
    FROM
    (VALUES
       ('corgis'),
       ('have'),
       ('100%'),
       ('more'),
       ('sploot!')
    ) as t(col1)
    WHERE
    col1 like '%';
  • SELECT *
    FROM
    (VALUES
       ('corgis'),
       ('have'),
       ('100%'),
       ('more'),
       ('sploot!')
    ) as t(col1)
    WHERE
    col1 like '%' ESCAPE '!';
  • SELECT *
    FROM
    (VALUES
       ('corgis'),
       ('have'),
       ('100%'),
       ('more'),
       ('sploot!')
    ) as t(col1)
    WHERE
    col1 like '%!%' ESCAPE '!';
  • SELECT *
    FROM
    (VALUES
       ('corgis'),
       ('have'),
       ('100%'),
       ('more'),
       ('sploot!')
    ) as t(col1)
    WHERE
    col1 like '!%' ESCAPE '!';
  • SELECT *
    FROM
    (VALUES
       ('corgis'),
       ('have'),
       ('100%'),
       ('more'),
       ('sploot!')
    ) as t(col1)
    WHERE
    col1 like '100!%' ESCAPE '!';

Learn more

Want to play around with the code in the quiz? Grab it here and have fun.

Interested in the official documentation on like and escaping? Docs are here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.