Difference between revisions of "Like"

From SQLZOO
Jump to navigation Jump to search
Line 1: Line 1:
How do I use LIKE in a sql SELECT statement.
How do I use LIKE in a sql SELECT statement.
<div class='ht'>
<div class='ht'>
<div class=params>schema:scott</div>
<div>
<div>
The LIKE command allows "Wild cards".
The LIKE command allows "Wild cards".
Line 9: Line 10:
The country Zambia matches because ambia matches with the %.
The country Zambia matches because ambia matches with the %.
</div>
</div>
<source lang=sql class='tidy'>CREATE TABLE bbc (name VARCHAR(10), region VARCHAR(10));
<source lang=sql class='tidy'>DROP TABLE bbc;
DROP TABLE actor;</source>
<source lang=sql class='setup'>CREATE TABLE bbc (name VARCHAR(10), region VARCHAR(10));
INSERT INTO bbc VALUES ('Poland', 'Europe');
INSERT INTO bbc VALUES ('Poland', 'Europe');
INSERT INTO bbc VALUES ('Zambia', 'Africa');
INSERT INTO bbc VALUES ('Japan', 'Asia');
</source>
INSERT INTO bbc VALUES ('Zambia', 'Africa');</source>
<source lang='sql' class='def e-access'>SELECT name FROM bbc
<source lang='sql' class='def e-access'>SELECT name FROM bbc
WHERE name LIKE 'Z*'</source>
WHERE name LIKE 'Z*'</source>

Revision as of 11:11, 12 July 2012

How do I use LIKE in a sql SELECT statement.

schema:scott

The LIKE command allows "Wild cards". A % may be used to match and string, _ will match any single character.

The example shows countries begining with Z. The country Zambia matches because ambia matches with the %.

DROP TABLE bbc;
DROP TABLE actor;
CREATE TABLE bbc (name VARCHAR(10), region VARCHAR(10));
INSERT INTO bbc VALUES ('Poland', 'Europe');
INSERT INTO bbc VALUES ('Japan', 'Asia');
INSERT INTO bbc VALUES ('Zambia', 'Africa');
SELECT name FROM bbc
WHERE name LIKE 'Z*'
SELECT name FROM bbc
WHERE name LIKE 'Z%'