DBA_oracle日期函式-【來自多個專案】

上帝_BayaiM發表於2024-08-30
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

相關文章