暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

oracle 也可以像sql server 一样不用to_date()做时间比较

原创 Anbob 2009-11-23
1072
以前用sql server 时,做时间比较 都 是直接用,比如 select * from tabname where datecol <'2010-11-23';
但是改用ORACLE以后我相信开始大家都会遇到为什么时间不可以对比了,结果才知道要用时间函数to_date()转换格式,今天做了个实验,也可以让它像SQL SERVER一样不用to_date()做时间比较,不过在此声明 ORACLE不建议使用隐式转换。
实验开始。。

[oracle@orazhang ~]$ sqlplus zhang/weizhao

SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 11月 24 00:13:56 2010

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production

With the Partitioning, OLAP and Data Mining options




SQL> create table test_date(id int,name varchar2(6),birthday date);

Table created.




SQL> insert into test_date values(1,'weizha',sysdate-365*20);

1 row created.

SQL> select * from test_date;

ID NAME         BIRTHDAY

---------- ------------ --------------

1 weizha       29-11月-90

SQL> commit;

Commit complete.



SQL> select * from test_date where birthday<'2010-1-1';

select * from test_date where birthday<'2010-1-1'

*

ERROR at line 1:

ORA-01861: literal does not match format string



SQL> alter session set nls_date_format='yyyy-mm-dd';

Session altered.

SQL> select * from test_date where birthday<'2010-1-1';

ID NAME         BIRTHDAY

---------- ------------ ----------

1 weizha       1990-11-29


实验结束

只要更改nls_date_format就可以,toad 上也是如此!同样number和char也存在隐式转换,再提示一下 不建议使用隐式转换!而使select * from test_date where birthday<to_date('2010-1-1','yyyy-mm-dd');
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论