SELECT ... FROM ... WHERE ... LIKE '%...%' |要显示的字段 |数据集 |条件字段 |检索内容 '%'只是用来进行模糊的。 可以根据你的需要来处理。 在SQL 查询器输入 SELECT 然后 Shift + F1 得到 Help 就可以了。
// From SQL Server 2000 Book On Line通配符 描述 示例 % 包含零个或更多字符的任意字符串。 WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名。 _(下划线) 任何单个字符。 WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。 [ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。 WHERE au_lname LIKE '[C-P]arsen' 将查找以arsen 结尾且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如,Carsen、Larsen、Karsen 等。 [^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。 WHERE au_lname LIKE 'de[^l]%' 将查找以 de 开始且其后的字母不为 l 的所有作者的姓氏。
LIKE *See note below * The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is a very powerful operator that allows you to select only rows that are "like" what you specify. The percent sign "%" can be used as a wild card to match any possible character that might appear before or after the characters specified. For example: select first, last, city from empinfo where first LIKE 'Er%'; This SQL statement will match any first names that start with 'Er'. Strings must be in single quotes. Or you can specify, select first, last from empinfo where last LIKE '%s'; This statement will match any last names that end in a 's'. select * from empinfo where first = 'Eric'; This will only select rows where the first name equals 'Eric' exactly.
CREATE PROCEDURE find_books @AU_LNAME char(20)ASSELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%'SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles tWHERE a.au_id = ta.au_id AND ta.title_id = t.title_idAND a.au_lname LIKE @AU_LNAME /////////////////////////////////////////////////////// USE pubs GO CREATE PROCEDURE find_books2 @au_lname varchar(20) AS SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @au_lname + '%' ///////////////////////////// EXEC find_books2 'ring' /////////////////////////// Here is the result set: title_id title -------- --------------------------------------------------------------- MC3021 The Gourmet Microwave PS2091 Is Anger the Enemy? PS2091 Is Anger the Enemy? PS2106 Life Without Fear (4 row(s) affected) ///////////////////////////////////////////////////////////////////// CREATE TABLE t (col1 char(30)) INSERT INTO t VALUES ('Robert King') SELECT * FROM t WHERE col1 LIKE '% King' -- returns 1 row -- Unicode pattern matching with nchar column CREATE TABLE t (col1 nchar(30)) INSERT INTO t VALUES ('Robert King') SELECT * FROM t WHERE col1 LIKE '% King' -- no rows returned -- Unicode pattern matching with nchar column and RTRIM CREATE TABLE t (col1 nchar (30)) INSERT INTO t VALUES ('Robert King') SELECT * FROM t WHERE RTRIM(col1) LIKE '% King' -- returns 1 row //////////////////////////////////////////////////////////////////////// SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'sys%' ///////////////////////////////////////////////////////// /////////////////////////////////////////////////////// Symbol Meaning LIKE ‘'5[%]’ 5% LIKE ‘[_]n’ _n LIKE ‘[a-cdf]’ a, b, c, d, or f LIKE ‘[-acdf]’ -, a, c, d, or f LIKE ‘[ [ ]’ [ LIKE ‘]’ ] LIKE ‘abc[_]d%’ abc_d and abc_de LIKE ‘abc[def]’ abcd, abce, and abcf Pattern Matching with the ESCAPE Clause //////////////////////////////////////////////////////////////// USE pubs GO SELECT notes FROM titles WHERE notes LIKE '50%% off when 100 or more copies are purchased' ESCAPE '%' GO ////////////////////////////////////////////////////////////////// Examples A. Use LIKE with the % wildcard character This example finds all phone numbers that have area code 415 in the authors table. USE pubs GO SELECT phone FROM authors WHERE phone LIKE '415%' ORDER by au_lname GO /////////////////////////////////////////////////////// Here is the result set: phone ------------ 415 658-9932 415 548-7723 415 836-7128 415 986-7020 415 836-7128 415 534-9219 415 585-4620 415 354-7128 415 834-2919 415 843-2991 415 935-4228 (11 row(s) affected) //////////////////////////////////////////////////////////////////// B. Use NOT LIKE with the % wildcard characterThis example finds all phone numbers in the authors table that have area codes other than 415. USE pubs GO SELECT phone FROM authors WHERE phone NOT LIKE '415%' ORDER BY au_lname GO ////////////////////////////////////////// Here is the result set: phone ------------ 503 745-6402 219 547-9982 615 996-8275 615 297-2723 707 938-6445 707 448-4982 408 286-2428 301 946-8853 801 826-0752 801 826-0752 913 843-0462 408 496-7223 (12 row(s) affected) /////////////////////////////////////////////////////////////////// C. Use the ESCAPE clauseThis example uses the ESCAPE clause and the escape character to find the exact character string 10-15% in column c1 of the mytbl2 table. USE pubs GO IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'mytbl2') DROP TABLE mytbl2 gO USE pubs GO CREATE TABLE mytbl2 ( c1 sysname ) GO INSERT mytbl2 VALUES ('Discount is 10-15% off') INSERT mytbl2 VALUES ('Discount is .10-.15 off') GO SELECT c1 FROM mytbl2 WHERE c1 LIKE '%10-15!% off%' ESCAPE '!' GO D. Use the [ ] wildcard charactersThis example finds authors with the first name of with Cheryl or Sheryl. USE pubs GO SELECT au_lname, au_fname, phone FROM authors WHERE au_fname LIKE '[CS]heryl' ORDER BY au_lname ASC, au_fname ASC GOThis example finds the rows for authors with last names of Carson, Carsen, Karson, or Karsen. USE pubs GO SELECT au_lname, au_fname, phone FROM authors WHERE au_lname LIKE '[CK]ars[eo]n' ORDER BY au_lname ASC, au_fname ASC GO
|要显示的字段
|数据集
|条件字段
|检索内容
'%'只是用来进行模糊的。
可以根据你的需要来处理。
在SQL 查询器输入 SELECT 然后 Shift + F1 得到 Help 就可以了。
% 包含零个或更多字符的任意字符串。 WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名。
_(下划线) 任何单个字符。 WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。
[ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。 WHERE au_lname LIKE '[C-P]arsen' 将查找以arsen 结尾且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如,Carsen、Larsen、Karsen 等。
[^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。 WHERE au_lname LIKE 'de[^l]%' 将查找以 de 开始且其后的字母不为 l 的所有作者的姓氏。
* The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is a very powerful operator that allows you to select only rows that are "like" what you specify. The percent sign "%" can be used as a wild card to match any possible character that might appear before or after the characters specified. For example: select first, last, city
from empinfo
where first LIKE 'Er%'; This SQL statement will match any first names that start with 'Er'. Strings must be in single quotes. Or you can specify, select first, last
from empinfo
where last LIKE '%s'; This statement will match any last names that end in a 's'. select * from empinfo
where first = 'Eric'; This will only select rows where the first name equals 'Eric' exactly.
///////////////////////////////////////////////////////
USE pubs
GO
CREATE PROCEDURE find_books2 @au_lname varchar(20)
AS
SELECT t.title_id, t.title
FROM authors a, titleauthor ta, titles t
WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id
AND a.au_lname LIKE @au_lname + '%'
/////////////////////////////
EXEC find_books2 'ring'
///////////////////////////
Here is the result set:
title_id title
-------- ---------------------------------------------------------------
MC3021 The Gourmet Microwave
PS2091 Is Anger the Enemy? PS2091 Is Anger the Enemy? PS2106 Life Without Fear (4 row(s) affected)
/////////////////////////////////////////////////////////////////////
CREATE TABLE t (col1 char(30))
INSERT INTO t VALUES ('Robert King')
SELECT *
FROM t
WHERE col1 LIKE '% King' -- returns 1 row
-- Unicode pattern matching with nchar column
CREATE TABLE t (col1 nchar(30))
INSERT INTO t VALUES ('Robert King')
SELECT *
FROM t
WHERE col1 LIKE '% King' -- no rows returned
-- Unicode pattern matching with nchar column and RTRIM
CREATE TABLE t (col1 nchar (30))
INSERT INTO t VALUES ('Robert King')
SELECT *
FROM t
WHERE RTRIM(col1) LIKE '% King' -- returns 1 row
////////////////////////////////////////////////////////////////////////
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'sys%'
/////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
Symbol Meaning
LIKE ‘'5[%]’ 5%
LIKE ‘[_]n’ _n
LIKE ‘[a-cdf]’ a, b, c, d, or f
LIKE ‘[-acdf]’ -, a, c, d, or f
LIKE ‘[ [ ]’ [
LIKE ‘]’ ]
LIKE ‘abc[_]d%’ abc_d and abc_de
LIKE ‘abc[def]’ abcd, abce, and abcf
Pattern Matching with the ESCAPE Clause
////////////////////////////////////////////////////////////////
USE pubs
GO
SELECT notes
FROM titles
WHERE notes LIKE '50%% off when 100 or more copies are purchased'
ESCAPE '%'
GO
//////////////////////////////////////////////////////////////////
Examples
A. Use LIKE with the % wildcard character
This example finds all phone numbers that have area code 415 in the authors table.
USE pubs
GO
SELECT phone
FROM authors
WHERE phone LIKE '415%'
ORDER by au_lname
GO
///////////////////////////////////////////////////////
Here is the result set:
phone
------------
415 658-9932
415 548-7723
415 836-7128
415 986-7020
415 836-7128
415 534-9219
415 585-4620
415 354-7128
415 834-2919
415 843-2991
415 935-4228
(11 row(s) affected)
////////////////////////////////////////////////////////////////////
B. Use NOT LIKE with the % wildcard characterThis example finds all phone numbers in the authors table that have area codes other than 415.
USE pubs
GO
SELECT phone
FROM authors
WHERE phone NOT LIKE '415%'
ORDER BY au_lname
GO
//////////////////////////////////////////
Here is the result set:
phone
------------
503 745-6402
219 547-9982
615 996-8275
615 297-2723
707 938-6445
707 448-4982
408 286-2428
301 946-8853
801 826-0752
801 826-0752
913 843-0462
408 496-7223
(12 row(s) affected)
///////////////////////////////////////////////////////////////////
C. Use the ESCAPE clauseThis example uses the ESCAPE clause and the escape character to find the exact character string 10-15% in column c1 of the mytbl2 table.
USE pubs
GO
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'mytbl2')
DROP TABLE mytbl2
gO
USE pubs
GO
CREATE TABLE mytbl2
(
c1 sysname
)
GO
INSERT mytbl2 VALUES ('Discount is 10-15% off')
INSERT mytbl2 VALUES ('Discount is .10-.15 off')
GO
SELECT c1
FROM mytbl2
WHERE c1 LIKE '%10-15!% off%' ESCAPE '!'
GO
D. Use the [ ] wildcard charactersThis example finds authors with the first name of with Cheryl or Sheryl.
USE pubs
GO
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_fname LIKE '[CS]heryl'
ORDER BY au_lname ASC, au_fname ASC
GOThis example finds the rows for authors with last names of Carson, Carsen, Karson, or Karsen.
USE pubs
GO
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_lname LIKE '[CK]ars[eo]n'
ORDER BY au_lname ASC, au_fname ASC
GO
如: like '%abc%'(包含abc的)等等