首页 » ORACLE 9i-23ai » oracle 也可以像sql server 一样不用to_date()做时间比较

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

以前用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’);
打赏

目前这篇文章有4条评论(Rss)评论关闭。

  1. Dorsey Mcroy | #1
    2011-12-21 at 04:51

    I used to be recommended this web site by means of my cousin. I am no longer certain whether this publish is written via him as nobody else understand such distinct approximately my problem. You are incredible! Thank you!

  2. Seston | #2
    2011-06-17 at 23:37

    That’s not just the best awnser. It’s the bestest answer!

  3. ugg boots online | #3
    2010-12-03 at 15:47

    这个不懂,看看就好。。

    • Romby | #4
      2011-06-16 at 19:46

      Great thninkig! That really breaks the mold!