DBA_oracle日期函数-【来自多个项目】

select  TO_DATE(trunc(F_GXSJ),'YYYY-MONTH-DD')  from fsxx_dx_log_new      where trunc(F_GXSJ)=TO_DATE()
select  trunc(F_GXSJ)  from fsxx_dx_log_new    -- 2012-10-26
select  SUBSTR(TO_CHAR(trunc(F_GXSJ),'YYYY-MM-DD'),6,2)  from fsxx_dx_log_new   --10
select  to_date(to_char(F_GXSJ,'yyyy-mm-dd'),'yyyy-mm-dd')  from fsxx_dx_log_new  --2012-10-26



select trunc(sysdate,'month') 本月第一天,
trunc(last_day(sysdate)) 本月最后一天,
trunc(add_months(sysdate,-1),'month') 上月第一天,
trunc(last_day(add_months(sysdate,-1))) 上月最后一天,
trunc(add_months(sysdate,-12),'month') 去年本月第一天,
trunc(last_day(add_months(sysdate,-12))) 去年本月最后一天 from dual  


select  TO_CHAR(trunc(F_GXSJ),'YYYY-MM-DD')  from fsxx_dx_log_new
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;   --   2012-12-20 14:20:27  
select to_char(sysdate,'yyyy') as nowYear   from dual;   //获取时间的年,2012
select to_char(sysdate,'year') as nowYear   from dual;   //获取时间的年,twenty twelve
select to_char(sysdate,'mm')    as nowMonth from dual;   //获取时间的月,12
select to_char(sysdate,'month')    as nowMonth from dual;   //获取时间的月,12月 
select to_char(sysdate,'dd')    as nowDay    from dual;  //获取时间的日 ,20 
select to_char(sysdate,'day')    as nowDay    from dual; //获取时间的星期几 ,星期四 
select to_char(sysdate,'hh24') as nowHour   from dual;   //获取时间的时  ,14
select to_char(sysdate,'mi')    as nowMinute from dual;   //获取时间的分 ,19
select to_char(sysdate,'ss')    as nowSecond from dual;   //获取时间的秒 ,50


5.0==trunc[截断到最接近的日期,单位为天] ,返回的是日期类型

select sysdate S1,                     
     trunc(sysdate) S2,              /*   //返回当前日期,无时分秒*/
     trunc(sysdate,'year') YEAR,     /*   //返回当前年的1月1日,无时分秒*/
     trunc(sysdate,'month') MONTH ,  /*   //返回当前月的1日,无时分秒*/
     trunc(sysdate,'day') DAY       /*    //返回当前星期的星期天,无时分秒*/
   from dual 
   
--   2012-12-20 14:28:35  2012-12-20  2012-1-1  2012-12-1  2012-12-16


6.0==round[舍入到最接近的日期](day:舍入到最接近的星期日)

select sysdate S1,
   round(sysdate) S2 ,
   round(sysdate,'year') YEAR,
   round(sysdate,'month') MONTH ,
   round(sysdate,'day') DAY from dual 

  --     2012-12-20 14:36:35    2012-12-21    2013-1-1    2013-1-1    2012-12-23
  


7.0=.查找月的第一天,最后一天

     SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
       Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
       Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
       LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month
   FROM dual; 
   
   --      2012-11-1    2012-11-30 23:59:59    2012-12-1    2012-12-31 23:59:59
   

case  when to_char(yhjksj,'yyyy-mm-dd')>='2014-05-22'
            and   to_char(yhjksj,'yyyy-mm-dd hh24')<'2014-05-22 04'
      then '5-22日4点缴款量'


 AND XZQH='320500' AND IFHT='0'  
 group by  to_char(YHJKSJ,'yyyy-mm-dd hh24')
 rq<trunc(sysdate-3)
YHJKSJ>=TO_DATE('2014-12-30','YYYY-MM-DD')  
YHJKSJ>=TO_DATE('2014-06-19 09:10','YYYY-MM-DD hh24:mi')

8.0=

select to_date(to_char(F_GXSJ,'yyyy-mm-dd'),'yyyy-mm-dd')-2 days   from fsxx_dx_log_new 
select to_date(to_char(F_GXSJ,'yyyy-mm-dd'),'yyyy-mm-dd')-2 months   from fsxx_dx_log_new 
select to_date(to_char(F_GXSJ,'yyyy-mm-dd'),'yyyy-mm-dd')-2 years   from fsxx_dx_log_new 



每年的1月1日:  Trunc(TO_DATE('2012-05-19','YYYY-MM-DD'), 'YEAR') 
没月的第一天:  Trunc(TO_DATE('2012-05-19','YYYY-MM-DD'), 'MONTH'

   select  
   TO_DATE('2018-06-19 09:10','YYYY-MM-DD hh24:mi'),
   TO_DATE('09:10','hh24:mi'),
   TO_DATE('2014-06-19','YYYY-MM-DD'),
   TO_CHAR((TO_DATE('2014-06-19 09:10','YYYY-MM-DD hh24:mi')),'YYYY-MM-DD hh24:mi')
   from dual 
   
   2014/6/19 9:10:00    2020/3/1 9:10:00    2014/6/19    2014-06-19 09:10

【sysdate】----------------------------------

select sysdate S1,
   TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi:ss') S2 ,
   TO_CHAR(sysdate,'YYYY-MM-DD') S2 , 
   TO_CHAR(sysdate,'hh24:mi') S2 ,
   TO_DATE(sysdate) S2,
   -- 【这个不可以,报错】 TO_DATE(sysdate,'YYYY-MM-DD hh24:mi')  S3  
   TO_DATE(TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi') ,'YYYY-MM-DD hh24:mi')  ss  -- 效果却是这个:2020/3/20 11:28:00
   -- 【这个不可以,报错】 TO_DATE(TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi') ,'hh24:mi')  ss
   -- 【这个不可以,报错】 TO_DATE(TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi') ,'YYYY-MM-DD')  ss
   from dual 
   

【结果】:
1    2020/3/20 11:32:34    2020-03-20 11:32    2020-03-20    11:32    2020/3/20    2020/3/20 11:32:00


【to_timestamp】------------------------------------------

select  
to_timestamp('2018-07-15 0:0:0', 'syyyy-mm-dd hh24:mi:ss'),
to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'),
to_timestamp(to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')

from  dual;

1 15-7月 -18 12.00.00.000000000 上午    2020-03-20 11:34:48    20-3月 -20 11.34.48.000000000 上午


TO_CHAR(extended_timestamp,'yyyy-mm-dd hh24:mi:ss') a1,
TO_DATE(TO_CHAR(extended_timestamp,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') 






oracle获得当前时间的,精确到毫秒   可以指定精确豪秒的位数
select to_char(systimestamp, 'yyyymmdd hh24:mi:ss.ff ') from dual;--20120516 11:56:40.729083

SELECT *  FROM   FJFL_JKXX_TEMP  WHERE HPHM='苏JRH248'   AND   TRUNC(YHJKSJ)=TO_DATE('2014-02-27','YYYY-MM-DD') 
select * from FJFL_POS_REG  as of timestamp sysdate-15/1440
SELECT * FROM FJFL_HTLOG T WHERE RQ>TO_DATE('2015-05-12','YYYY-MM-DD') AND XZQH='321200' ORDER BY RQ  DESC
update fjfl_zt set zt='00000',rq=to_date('2013-12-09 00:00:00','yyyy-mm-dd hh24:mi:ss')

该语句可以查看每天产生日志的多少,估计我们应用的日志量,可以估计归档的大小。
select to_char(FIRST_TIME,'yyyy/mm/dd') day,count(*) from  v$log_history  group by to_char(FIRST_TIME,'yyyy/mm/dd')
order by day  desc;

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

SCN转换成时间:
select to_char(scn_to_timestamp(3998591352171),'YYYY-MM-DD HH24:MI:SS') from dual;

时间转换成SCN:
select timestamp_to_scn(to_date('2011-04-14 11:10:25','YYYY-MM-DD HH24:MI:SS')) from dual

 

posted @ 2024-08-30 10:05  上帝_BayaiM  阅读(2)  评论(0编辑  收藏  举报