SQL like

Determining whether a given string matches the pattern specified. A pattern can include regular characters and wildcard characters.

Pattern matching process, regular characters must specify the exact match character string. However, any fragment can be used to match the wildcard character string.

Using =, and! = String comparison operators compared LIKE operator can use wildcards more flexible.

If any parameters are not part of a string data type, Microsoft® SQL Server ™ will convert a string data type (if possible).
grammar
match_expression_r [ NOT ] LIKE pattern [ ESCAPE escape_character ]
parameter
match_expression_r
Effective SQL Server expression of any string data type.
pattern
match_expression_r in the search mode, you can include the following valid SQL Server wildcard.
Wildcard description example
% Any string of zero or more characters. WHERE title LIKE '% computer%' finds all titles containing the word computer anywhere in the title of the.
_ (Underscore) Any single character. WHERE au_fname LIKE '_ean' will look at all the four-letter name ending with ean (Dean, Sean, etc.).
[] Specified range ([AF]) or set ([abcdef]) in any single character. WHERE au_lname LIKE '[CP] arsen' finds and ending arsen

Any single character between C and P in the beginning of the last name, e.g., Carsen, Larsen, Karsen like.

[^] Does not belong to the specified range ([AF]) or set ([abcdef]) any single character. WHERE au_lname LIKE 'de [^ l]%' will look at de start and subsequent letters are not all l author's last name.
escape_character
Any valid SQL Server expression of all data types string data type classification. escape_character no default, and must contain only one character.
Result Type
Boolean
The result value
If match_expression_r match a specified pattern, LIKE returns TRUE.
Note
When using the LIKE string comparison, all the characters in the pattern string have meaning, including starting or trailing spaces. If the query in comparison to return

All lines "abc" (abc after a space), and will not return contains "abc" (abc after no space) row of columns.

But you can ignore trailing spaces pattern to match the expression. If the comparison to return to contain "abc" (abc after no space) of all the rows, the query

It will return to "abc" and began with zero or more rows all trailing spaces.

Due to the way data is stored, containing char and string varchar data pattern comparison may not by comparing LIKE.

Understand each type of data storage and the cause of the failure LIKE comparison is very important. The following example char variable is passed to the local storage process,

Then use pattern matching to find all the writings of an author. In this process, the author's last name will be passed as a variable.

When the number of characters in the name contains less than 20, char variable (@AU_LNAME) will contain a trailing space, which leads to find_books process no rows are returned.

Because au_lname as varchar type, so there is no trailing spaces. Because trailing spaces are significant, so this process fails.

Pattern matching using LIKE
When you search for datetime values, it is recommended to use LIKE, because datetime entries may contain a variety of date parts. For example, if the value 19981231 9:20
Into a column named arrival_time, the clause WHERE arrival_time = 9:20 9:20 will not find an exact match of the string,

因为 SQL Server 将其转换为 1900 年 1 月 1 日上午 9:20。然而,子句 WHERE arrival_time LIKE '%9:20%' 将找到匹配。

LIKE 支持 ASCII 模式匹配和 Unicode 模式匹配。当所有参数,包括 match_expression_r、pattern 和
escape_character(如果有)都是 ASCII 字符数据类型时,将执行 ASCII 模式匹配。如果其中任何参数属于 Unicode 数据类型,

则所有参数将被转换为 Unicode 并执行 Unicode 模式匹配。当对 Unicode 数据(nchar 或 nvarchar 数据类型)使用 LIKE 时,

尾随空格是有意义的。但是对于非 Unicode 数据,尾随空格没有意义。Unicode LIKE 与 SQL-92 标准兼容。

ASCII LIKE 与 SQL Server 的早期版本兼容。

使用 % 通配符
如果指定 LIKE '5%',SQL Server 将搜索后面带有零个或多个任意字符的数字 5。
例如,此查询将显示数据库中所有的系统表,因为它们都以字母 sys 开始:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'sys%'
说明 请注意:系统表可以随版本不同而更改。推荐使用信息架构视图或适用的存储过程处理 SQL Server 系统表。
若要查阅非系统表的所有对象,请使用 NOT LIKE 'sys%'。如果共有 32 个对象且 LIKE 找到 13 个与模式匹配的名称,

则 NOT LIKE 将找到 19 个与 LIKE 模式不匹配的对象。

使用 LIKE '[^s][^y][^s]%' 模式不一定每次找到的名称都相同。可能仅得到 14 个名称(而不是 19 个),除了系统表名称外,

所有以 s 开始或第二个字母为 y 或第三个字母为 s 的名称也都将从结果中消除。

这是因为用反向通配符匹配字符串是分步骤进行计算的,一次一个通配符。

如果在计算过程中任一环节匹配失败,那么就会将其消除。

Guess you like

Origin www.cnblogs.com/xtwkh1973/p/10990568.html