oracle - 关于日期的函数

不支持 :2013/4/21 24:00:00 格式
            --2013/4/21 0:00:01 是属于4月22日(有点郁闷)

.当用到函数时,日期转换里面的格式化参数是不区分大小的,例如,Select to_char(t.vdate,'dd'),t.vdate From test_student t;

'yyyy-mm-dd hh24:mi:ss' 和'YYYY-MM-DD HH24:MI:SS'、'yyyy-MM-dd hh24:mi:ss'、'dd'、'DD'等都没区别

 

1.日期到字符:

select sysdate,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual

timestamp类型转字符:30-6月 -14 01.42.31.865000 下午 +08:00

select to_char(current_timestamp,'DD-MON-YYYY HH24:MI:SSxFF3') from dual;   这里的数字3,填几精度就是几,最大=9

select to_timestamp('2000-1-1 0:0:0.1234', 'syyyy-mm-dd hh24:mi:ss.ff') from dual;

select t.v_time,to_char(current_timestamp(8),'DD-MON-YYYY HH24:MI:SSxFF')
 from test_student t;

2.字符到日期:

select to_date('2013-03-13 21:15:37','yyyy-mm-dd hh24:mi:ss') from dual

3.--查询日期大于或小于某段时间,两个方案,一是两边都转换成时间类型,二是都转换成varchar类型
select SYSDATE from dual where to_char(sysdate,'YYYYMMDD') > '20050101';
select SYSDATE from dual where sysdate > to_date('20050101','YYYYMMDD');

3.add_months

 select to_char(add_months(sysdate, -1), 'yyyymm') from dual;

现在sysdate是11月,如果需要取上一个月的数据,并且每天都要进行此操作,那就每天都需要改时间,用add_months就解决这个问题了

add_months(time,months)函数可以得到某一时间之前或之后n个月的时间

  1. 如 select add_months(sysdate,-6) from dual;

该查询的结果是当前时间半年前的时间

     2.select add_months(sysdate,6) from dual;

该查询的结果是当前时间半年后的时间

 

4.select TRUNC(SYSDATE) from dual ;--2012/11/19

 --查询在某段日期之间的数据

 select * from all_tables a
WHERE A.LAST_ANALYZED IS NOT NULL
and a.LAST_ANALYZED between to_date('2013-03-01','yyyy-mm-dd') and to_date('2013-04-01','yyyy-mm-dd')
order by a.LAST_ANALYZED desc;

 

http://blog.csdn.net/zhangyun123/article/details/2805698

http://oracle.chinaitlab.com/exploiture/823866.html

posted on 2012-11-19 17:16  lovebeauty  阅读(329)  评论(0编辑  收藏  举报

导航