QUESTION 3
You need to extract details of those products in the SALES table where the PROD_ID column contains
the string '_D123'.
Which WHERE clause could be used in the SELECT statement to get the required output?
A. WHERE prod_id LIKE '%_D123%' ESCAPE '_'
B. WHERE prod_id LIKE '%\_D123%' ESCAPE '\'
C. WHERE prod_id LIKE '%_D123%' ESCAPE '%_'
You need to extract details of those products in the SALES table where the PROD_ID column contains
the string '_D123'.
Which WHERE clause could be used in the SELECT statement to get the required output?
A. WHERE prod_id LIKE '%_D123%' ESCAPE '_'
B. WHERE prod_id LIKE '%\_D123%' ESCAPE '\'
C. WHERE prod_id LIKE '%_D123%' ESCAPE '%_'
D. WHERE prod_id LIKE '%\_D123%' ESCAPE '\_'
答案:B
解析:
SQL> create table sales (
2 prod_id varchar2(10) not null,
3 cust_id number not null,
4 time_id date not null,
5 channel_id number not null,
6 promo_id number not null,
7 quantity_sold number(10,2) not null);
表已创建。
SQL> insert into sales values('1_D123',2,sysdate,3,4,5);
已创建 1 行。
--A选项,报转义符之后字符缺失或非法
SQL> select * from sales
2 where prod_id LIKE '%_D123%' ESCAPE '_'
3 ;
where prod_id LIKE '%_D123%' ESCAPE '_'
*
第 2 行出现错误:
ORA-01424: 转义符之后字符缺失或非法
--B选项,可以正确的查出来
SQL> select * from sales
2 WHERE prod_id LIKE '%\_D123%' ESCAPE '\'
3 ;
PROD_ID CUST_ID TIME_ID CHANNEL_ID PROMO_ID QUANTITY_SO
---------- ---------- -------------- ---------- ---------- -----------
1_D123 2 27-12月-13 3 4
--C选项报错转义符必须是长度为 1 的字符串
SQL> select * from sales
2 WHERE prod_id LIKE '%_D123%' ESCAPE '%_';
WHERE prod_id LIKE '%_D123%' ESCAPE '%_'
*
第 2 行出现错误:
ORA-01425: 转义符必须是长度为 1 的字符串
--D选项报错转义符必须是长度为 1 的字符串
SQL> select * from sales
2 WHERE prod_id LIKE '%\_D123%' ESCAPE '\_';
WHERE prod_id LIKE '%\_D123%' ESCAPE '\_'
*
第 2 行出现错误:
ORA-01425: 转义符必须是长度为 1 的字符串
SQL LIKE与ESCAPE子句
本文探讨了如何使用SQL中的LIKE与ESCAPE子句来精确匹配包含特殊字符的字符串。通过具体示例展示了不同ESCAPE字符的使用方法及其可能产生的错误。
1234

被折叠的 条评论
为什么被折叠?



