oracle 中使用like的問題
今天我想查詢開發人員建立主鍵時使用PK__開頭的索引,發現執行如下語句會查詢不是兩個PK__開頭的語句:
SELECT *
FROM dba_indexes
WHERE owner = 'ICARE' AND index_name LIKE 'PK__%'
ORDER BY 2
很明顯,_在like中表示任意字元,但是如何使用轉義呢?
google查詢了一下發現如下連結記錄下來:
http://www-eleves-isia.cma.fr/documentation/OracleDoc/LIKE-OPERATOR.html
但願\在blog中正常,修改為如下,執行就正確了:
SELECT *
FROM dba_indexes
WHERE owner = &o AND index_name LIKE 'PK__%' ESCAPE ''
ORDER BY 2
LIKE-OPERATOR
LIKE OPERATOR
The LIKE operator is used in character string comparisons with
pattern matching. The syntax for a condition using the LIKE
operator is shown in this diagram:
SYNTAX:
char1 [NOT] LIKE char2 [ESCAPE 'c']
where:
char1
is a value to be compared with a pattern. This value can have
datatype CHAR or VARCHAR2.
NOT
logically inverts the result of the condition, returning FALSE if
the condition evaluates to TRUE and TRUE if it evaluates to FALSE.
char2
is the pattern to which char1 is compared. The pattern is a value
of datatype CHAR or VARCHAR2 and can contain the special pattern
matching characters % and _.
ESCAPE
identifies a single character as the escape character. The escape
character can be used to cause Oracle to interpret % or _ literally,
rather than as a special character, in the pattern.
If you wish to search for strings containing an escape character, you
must specify this character twice. For example, if the escape
character is '/', to search for the string 'client/server', you must
specify, 'client//server'.
While the equal (=) operator exactly matches one character value to
another, the LIKE operator matches a portion of one character value
to another by searching the first value for the pattern specified by
the second.
With the LIKE operator, you can compare a value to a pattern rather
than to a constant. The pattern can only appear after the LIKE
keyword. For example, you can issue the following query to find the
salaries of all employees with names beginning with 'SM':
SELECT sal
FROM emp
WHERE ename LIKE 'SM%'
The following query finds the salaries of all employees with the
name 'SM%', since the query uses the equality operator instead of
the LIKE operator:
SELECT sal
FROM emp
WHERE ename = 'SM%'
The following query finds the salaries of all employees with the
name 'SM%'. Oracle interprets 'SM%' as a text literal, rather than
as a pattern, because it precedes the LIKE operator:
SELECT sal
FROM emp
WHERE 'SM%' LIKE ename
Patterns often use special characters that Oracle matches with
different characters in the value:
* An underscore (_) in the pattern matches exactly one character (as
opposed to one byte in a multi-byte character set) in the value.
* A percent sign (%) in the pattern can match zero or more
characters (as opposed to bytes in a multi-byte character set) in
the value. Note that the pattern '%' cannot match a null.
Case is significant in all conditions comparing character
expressions including the LIKE and equality (=) operators. You can
use the UPPER() function to perform a case insensitive match, as in
this condition:
UPPER(ename) LIKE 'SM%'
When LIKE is used to search an indexed column for a pattern, the
performance benefit associated with the index is lost if the first
character in the pattern is % or _. If the leading character in the
pattern is not % or _, there is some performance benefit to the
index because Oracle can restrict the comparison to rows known to
begin with the specified first character.
EXAMPLE I:
This condition is true for all ENAME values beginning with MA:
ename LIKE 'MA%'
All of these ENAME values make the condition TRUE:
MARTIN, MA, MARK, MARY
Since case is significant, ENAME values beginning with Ma, ma, and
mA make the condition FALSE.
EXAMPLE II:
Consider this condition:
ename LIKE 'SMITH_'
This condition is true for these ENAME values:
SMITHE, SMITHY, SMITHS
This condition is false for 'SMITH', since the special character _
must match exactly one character of the ENAME value.
THE ESCAPE OPTION:
You can include the actual characters % or _ in the pattern by using
the ESCAPE option. The ESCAPE option identifies the escape
character. If the escape character appears in the pattern before
the character % or _, Oracle interprets this character literally in
the pattern, rather than as a special pattern matching character.
EXAMPLE III:
To search for any employees with the character string 'A_B' in their
name:
SELECT ename
FROM emp
WHERE ename LIKE '%A_B%' ESCAPE ''
The ESCAPE option identifies the backslash () as the escape
character. In the pattern, the escape character precedes the
underscore (_). This causes Oracle to interpret the underscore
literally, rather than as a special pattern matching character.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-82895/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle 解決like中無法匹配下劃線的問題Oracle
- Laravel 框架中 whereRaw like 引數繫結問題Laravel框架
- MySQL 中文 like 問題解決MySql
- 問題:mybatis like 佔位符處理MyBatis
- laravel使用中遇到的問題Laravel
- oracle 使用nullif解決除數為零的問題OracleNull
- 使用DBUA升級 Oracle 11.2.0.4到Oracle 19C的問題記錄Oracle
- weex使用中的問題記錄
- Typora 使用中的幾個問題
- thrift使用過程中的問題
- Oracle資料庫中的逐行處理問題NEOracle資料庫
- Oracle trigger問題Oracle
- Oracle的SCN顯示問題Oracle
- ORACLE RAC TO RAC DG搭建過程中可能遇到的問題Oracle
- Vue使用中遇到的程式碼問題Vue
- 使用ogg 從oracle 同步mysql遇到問題記錄OracleMySql
- Oracle CPU使用率過高問題處理Oracle
- 分割槽使用與Oracle許可證問題XSOracle
- Oracle常用傻瓜問題1000問Oracle
- [20230908]Oracle Index Range Scan with LIKE Condition on Wildcard '_'.txtOracleIndex
- Oracle的時區問題Time ZoneOracle
- oracle正規表示式regexp_like的用法詳解Oracle
- 如何處理Oracle資料庫中的壞塊問題(轉)Oracle資料庫
- LVGL 中圖片使用問題
- pycharm+python使用中的相關問題PyCharmPython
- autohotkey qt程式中無法使用的問題QT
- NBU異機還原oracle,使用者不同問題Oracle
- [20191202]關於oracle例項是否使用hugepages問題.txtOracle
- 一次詭異的Oracle使用者無法su問題Oracle
- Oracle dblink監聽問題Oracle
- 使用 pytz 處理 Python 中的時區問題Python
- SpringBoot中關於Mybatis使用的三個問題Spring BootMyBatis
- redis使用中存在的問題及如何避免(二)Redis
- redis使用中存在的問題及如何避免(一)Redis
- Spring中多執行緒的使用及問題Spring執行緒
- Oracle中job的使用詳解Oracle
- ORACLE中%TYPE和%ROWTYPE的使用Oracle
- Oracle中Decode()函式的使用Oracle函式
- ORACLE RAC中連線ScanIP報錯ORA-12545的問題解決Oracle