53. View the Exhibit and examine the data in the PROMOTIONS table.
PROMO_BEGIN_DATE is stored in the default date format, dd-mon-rr.
You need to produce a report that provides the name, cost, and start date of all promos in the POST
category that were launched before January 1, 2000.
Which SQL statement would you use?
A. SELECT promo_name, promo_cost, promo_begin_date
FROM promotions
WHERE promo_category = 'post' AND promo_begin_date < '01-01-00';
B. SELECT promo_name, promo_cost, promo_begin_date
FROM promotions
WHERE promo_cost LIKE 'post%' AND promo_begin_date < '01-01-2000';
C. SELECT promo_name, promo_cost, promo_begin_date
FROM promotions
WHERE promo_category LIKE 'P%' AND promo_begin_date < '1-JANUARY-00'; 可以,但是like条件不满足
D. SELECT promo_name, promo_cost, promo_begin_date
FROM promotions
WHERE promo_category LIKE '%post%' AND promo_begin_date < '1-JAN-00';
Answer: D
答案解析:
在表达式中,Oracle Server 可以自动执行以下转换:
从 VARCHAR2 或CHAR隐式转换为NUMBER
从VARCHAR2 或CHAR隐式转换为DATE,但必须符合日期的规定的格式。
Oracle Server 可以在表达式中自动执行数据类型转换。例如,表达式hire_date > '01-JAN-90' 将导致字符串'01-JAN-90' 隐式转换为一个日期。因此,表达式中的VARCHAR2 或CHAR 值可以隐式转换为数字或日期数据类型。
A,错误,需要视同to_date显示转换
实验验证:
首先确认默认的时间格式:
sys@TEST0924> select sysdate from dual;
SYSDATE
------------------
17-OCT-13
A,
sh@TEST0924> SELECT promo_name, promo_cost, promo_begin_date FROM promotions WHERE promo_category = 'post' AND promo_begin_date < '01-01-00';
SELECT promo_name, promo_cost, promo_begin_date FROM promotions WHERE promo_category = 'post' AND promo_begin_date < '01-01-00'
*
ERROR at line 1:
ORA-01843: not a valid month
显示转换后:
sh@TEST0924> SELECT promo_name, promo_cost, promo_begin_date FROM promotions WHERE promo_category = 'post' AND promo_begin_date <to_date( '01-01-00','dd-mm-yy');
PROMO_NAME PROMO_COST PROMO_BEGIN_DATE
------------------------------ ---------- ------------------
post promotion #20-232 300 25-SEP-98
post promotion #21-166 2000 25-SEP-98
post promotion #20-449 4200 10-NOV-98
B,
sh@TEST0924> SELECT promo_name, promo_cost, promo_begin_date FROM promotions WHERE promo_cost LIKE 'post%' AND promo_begin_date < '01-01-2000';
no rows selected
C,
sh@TEST0924> SELECT promo_name, promo_cost, promo_begin_date FROM promotions WHERE promo_category LIKE 'P%' AND promo_begin_date < '1-JANUARY-00'
2 ;
no rows selected
D,
sh@TEST0924> SELECT promo_name, promo_cost, promo_begin_date FROM promotions WHERE promo_category LIKE '%post%' AND promo_begin_date < '1-JAN-00';
PROMO_NAME PROMO_COST PROMO_BEGIN_DATE
------------------------------ ---------- ------------------
post promotion #20-232 300 25-SEP-98
post promotion #21-166 2000 25-SEP-98
post promotion #20-449 4200 10-NOV-98