请教各位大大如何将DATE时间格式
betweenask_date between to_date('2010-11-09 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date('2010-11-09 23:59:59','yyyy-mm-dd hh24:mi:ss')不转换为CHAR的情况下 实现取前一天时间and to_char(a.ask_date, 'yyyymmdd') = to_char(sysdate -1, 'yyyymmdd')如实现上面SQL功效,
betweenask_date between to_date('2010-11-09 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date('2010-11-09 23:59:59','yyyy-mm-dd hh24:mi:ss')不转换为CHAR的情况下 实现取前一天时间and to_char(a.ask_date, 'yyyymmdd') = to_char(sysdate -1, 'yyyymmdd')如实现上面SQL功效,
SQL> select sysdate-1 from dual;SYSDATE-1
--------------
01-12月-10
and trunc(a.ask_date, 'dd') = trunc(sysdate -1, 'dd')
betweenask_date between to_date('2010-11-09 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date('2010-11-09 00:00:00','yyyy-mm-dd hh24:mi:ss')-1
相当慢可能是你ask_date上使用了函数,即使ask_date上有索引,也不能使用上索引。
所以尽量不要在列上使用函数,取当前时间前一天:
and a.ask_date between trunc(sysdate-1) and trunc(sydate)如果确实需要转换,你可以ask_date上建立一个函数索引,如:
create index idx_ask_date on your_table(to_char(a.ask_date, 'yyyymmdd'));
ask_date between to_date('2010-11-09 00:00:00','yyyy-mm-dd hh24:mi:ss')-1
and to_date('2010-11-09 23:59:59','yyyy-mm-dd hh24:mi:ss')-1
问题理解描述:你的目的是取前一天的数据,假设今天为2010-11-10(sysdate),你要取的数据如上,再不转换个时的前提下你可以如此写:
ask_date between trunc(sysdate) -1 and trunc(sysdate)