有关 Oracle 日期字段的查询问题(30分)

  • 主题发起人 王少东
  • 开始时间

王少东

Unregistered / Unconfirmed
GUEST, unregistred user!
环境:Oracle 8.0.5 中文版(即:安装时选择语言 Simple Chinese 而不是 English)
执行: select sysdate from dual;
得到日期显示格式为: 17-11月-02
问题是:
如何通过匹配符来查询某一天、某一月、某一年的记录?
如英文版下的查询:
select * from emp where hiredate like '12 Mar 2002' ---- 某一天
select * from emp where hiredate like '__ Mar 2002' ---- 某一月
select * from emp where hiredate like '__ ___ 2002' ---- 某一年
希望那位朋友知道的话,给个答案,谢谢!!
 
不使用匹配字符,而是使用比较字符,使用To_Date首先将需要比较的转换成日期类型,然后进行比较。
如果要局部匹配,可以取出其某一部分进行比较。
trunc可以,to_number也可以。类似
select to_number(to_char(sysdate,'yyyy')) from dual;
 
select * from emp where hiredate = to_date('2002-12-1','YYYY-MM-DD') ---某一天
select * from emp where to_char('YYYY-MM',hiredate) = '2002-02' ---某一月
select * from emp where to_char('yyyy',hiredate)='2002' ---某一年
 
上面两位的SQL将会引起Full Table Scan。
匹配符查询我认为很难办到。
最好还是 >= And <= 来条件查询。
 
顶部