当前位置:操作系统 > Unix/Linux >>

[每日一题] OCP1z0-047 :2013-07-18时间格式

[每日一题] OCP1z0-047 :2013-07-18时间格式
 
 
这道题目是考时间格式,日期的存贮格式,在ORACLE内部都由如下七部分构成:世纪、年、月、日、小时、分钟、秒。
OK,我们来做个测试:
gyj@OCM> create table gyj_order(order_id int,order_date date);
Table created.
gyj@OCM> insert into gyj_order values(1,sysdate);

1 row created.
gyj@OCM> commit;
Commit complete.
gyj@OCM> select * from gyj_order where order_date> to_date('JUL 10  2006','MON DDYYYY');

  ORDER_IDORDER_DAT
---------- ---------
         116-JUL-13
说明答案A正确

gyj@OCM> select * from gyj_order whereto_char(order_date,'MON DD YYYY')='JUL 16 2013';

  ORDER_IDORDER_DAT
---------- ---------
         116-JUL-13
说明答案B正确

gyj@OCM> select to_char(add_months(sysdate,6),'MONDD YYYY') from dual;

TO_CHAR(ADD_MO
--------------
JAN 16 2014
gyj@OCM> select * from gyj_order whereorder_date> to_char(add_months(sysdate,6),'MON DD YYYY');
select * from gyj_order where order_date>to_char(add_months(sysdate,6),'MON DD YYYY')
                                          *
ERROR at line 1:
ORA-01858: a non-numeric character was found where anumeric was expected

 

说明答案C不正确,时间类型与字符类型不能做比较
 
gyj@OCM> select * from gyj_order where order_datein(to_date('JUL 16 2013','MON DD YYYY'),to_char('JUL 16 2013','MON DD YYYY'));
select * from gyj_order where order_datein(to_date('JUL 16 2013','MON DD YYYY'),to_char('JUL 16 2013','MON DD YYYY'))
                                                                                        *
ERROR at line 1:
ORA-01722: invalid number


gyj@OCM> select to_char('JUL 16 2013','MON DDYYYY') from dual; select to_char('JUL 16 2013','MON DD YYYY') from dual
              *
ERROR at line 1:
ORA-01722: invalid number

 

上面的写法是错的,已是字符就无须用to_char转化,看下面的写法才是正确的:
 
gyj@OCM> select to_char(to_date('JUL 16 2013','MONDD YYYY'),'MON DD YYYY') from dual;

TO_CHAR(TO_
-----------
JUL 16 2013

 

 
还有时间与字符类型不能直接计算,如下报错:
gyj@OCM> select * from gyj_order where order_datein(to_char(to_date('JUL 16 2013','MON DD YYYY'),'MON DD YYYY'));
select * from gyj_order where order_datein(to_char(to_date('JUL 16 2013','MON DD YYYY'),'MON DD YYYY'))
                                            *
ERROR at line 1:
ORA-01858: a non-numeric character was found where anumeric was expected

 

 
答案D不正确
 
最终正确答案选:AB
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,