欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Oracle之TO_DATE用法詳解

 更新時(shí)間:2021年08月11日 14:47:14   作者:weixin_33994444  
這篇文章主要介紹了Oracle之TO_DATE用法詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

TO_DATE格式(以時(shí)間:2007-11-02 13:45:25為例)

Year:

yy       two digits   兩位年                顯示值:07

yyy     three digits 三位年                顯示值:007

yyyy   four digits  四位年                顯示值:2007

Month:

mm      number          兩位月                   顯示值:11

mon     abbreviated   字符集表示            顯示值:11月,若是英文版,顯示nov

month  spelled out    字符集表示            顯示值:11月,若是英文版,顯示november

Day:

dd      number          當(dāng)月第幾天             顯示值:02

ddd    number          當(dāng)年第幾天             顯示值:02

dy      abbreviated    當(dāng)周第幾天簡(jiǎn)寫     顯示值:星期五,若是英文版,顯示fri

day    spelled out      當(dāng)周第幾天全寫     顯示值:星期五,若是英文版,顯示friday 

Hour:

hh       two digits     12小時(shí)進(jìn)制        顯示值:01

hh24   two digits     24小時(shí)進(jìn)制        顯示值:13

Minute:

mi    two digits      60進(jìn)制            顯示值:45

Second:

ss    two digits      60進(jìn)制            顯示值:25

其它

Q        digit           季度                    顯示值:4

WW    digit           當(dāng)年第幾周         顯示值:44

W        digit           當(dāng)月第幾周         顯示值:1

24小時(shí)格式下時(shí)間范圍為: 0:00:00 - 23:59:59....

12小時(shí)格式下時(shí)間范圍為: 1:00:00 - 12:59:59....

1. 日期和字符轉(zhuǎn)換函數(shù)用法(to_date,to_char)

    
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;   //日期轉(zhuǎn)化為字符串  
select to_char(sysdate,'yyyy')  as nowYear   from dual;   //獲取時(shí)間的年  
select to_char(sysdate,'mm')    as nowMonth  from dual;   //獲取時(shí)間的月  
select to_char(sysdate,'dd')    as nowDay    from dual;   //獲取時(shí)間的日  
select to_char(sysdate,'hh24')  as nowHour   from dual;   //獲取時(shí)間的時(shí)  
select to_char(sysdate,'mi')    as nowMinute from dual;   //獲取時(shí)間的分  
select to_char(sysdate,'ss')    as nowSecond from dual;   //獲取時(shí)間的秒

2. 字符串和時(shí)間互轉(zhuǎn)

select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual
select to_char( to_date(222,'J'),'Jsp') from dual //顯示Two Hundred Twenty-Two    

3.求某天是星期幾

select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;     //星期一     
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day',
'NLS_DATE_LANGUAGE = American') from dual;   // monday   
//設(shè)置日期語(yǔ)言     
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';     
//也可以這樣     
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')    

4. 兩個(gè)日期間的天數(shù)

select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;

5. 時(shí)間為null的用法

select id, active_date from table1     
UNION     
select 1, TO_DATE(null) from dual;  //注意要用TO_DATE(null)    

6.月份差

a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')     
//那么12月31號(hào)中午12點(diǎn)之后和12月1號(hào)的12點(diǎn)之前是不包含在這個(gè)范圍之內(nèi)的。     
//所以,當(dāng)時(shí)間需要精確的時(shí)候,覺得to_char還是必要的

7. 日期格式?jīng)_突問題

輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: '01-Jan-01'

alter system set NLS_DATE_LANGUAGE = American     
alter session set NLS_DATE_LANGUAGE = American     
//或者在to_date中寫     
select to_char(to_date('2002-08-26','yyyy-mm-dd'),
   'day','NLS_DATE_LANGUAGE = American') from dual;     
//注意我這只是舉了NLS_DATE_LANGUAGE,當(dāng)然還有很多,可查看     
select * from nls_session_parameters     
select * from V$NLS_PARAMETERS    

8.查詢特殊條件天數(shù)

select count(*)     
from ( select rownum-1 rnum     
   from all_objects     
   where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-     
   02-01','yyyy-mm-dd')+1     
  )     
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )     
    not in ( '1', '7' )     
//查找2002-02-28至2002-02-01間除星期一和七的天數(shù)     
//在前后分別調(diào)用DBMS_UTILITY.GET_TIME, 讓后將結(jié)果相減(得到的是1/100秒, 而不是毫秒).    

9. 查找月份

select months_between(to_date('01-31-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;     
//結(jié)果為:1     
select months_between(to_date('02-01-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;     
//結(jié)果為:1.03225806451613

10. Next_day的用法

Next_day(date, day)     
Monday-Sunday, for format code DAY     
Mon-Sun, for format code DY     
1-7, for format code D    

11.獲得小時(shí)數(shù)

//extract()找出日期或間隔值的字段值
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer     
select sysdate ,to_char(sysdate,'hh') from dual;     
 
SYSDATE               TO_CHAR(SYSDATE,'HH')     
-------------------- ---------------------     
2003-10-13 19:35:21   07     
select sysdate ,to_char(sysdate,'hh24') from dual;     
 
SYSDATE               TO_CHAR(SYSDATE,'HH24')     
-------------------- -----------------------     
2003-10-13 19:35:21   19    

12.年月日的處理

SELECT
  older_date,
  newer_date,
  years,
  months,
  ABS (
    TRUNC (
      newer_date - ADD_MONTHS (older_date, years * 12 + months)
    )
  ) days
FROM
  (
    SELECT
      TRUNC (
        MONTHS_BETWEEN (newer_date, older_date) / 12
      ) YEARS,
      MOD (
        TRUNC (
          MONTHS_BETWEEN (newer_date, older_date)
        ),
        12
      ) MONTHS,
      newer_date,
      older_date
    FROM
      (
        SELECT
          hiredate older_date,
          ADD_MONTHS (hiredate, ROWNUM) + ROWNUM newer_date
        FROM
          emp
      )
  )   

13.處理月份天數(shù)不定的辦法

select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual    

14.找出今年的天數(shù)

select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual    
   //閏年的處理方法     
  to_char( last_day( to_date('02'    | | :year,'mmyyyy') ), 'dd' )     
   //如果是28就不是閏年 
 

15.yyyy與rrrr的區(qū)別

   YYYY99  TO_C     
   ------- ----     
   yyyy 99 0099     
   rrrr 99 1999     
   yyyy 01 0001     
   rrrr 01 2001   

16.不同時(shí)區(qū)的處理

 select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,
   sysdate   from dual;    

17. 5秒鐘一個(gè)間隔

   Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,
   TO_CHAR(sysdate,'SSSSS')   from dual    
   //2002-11-1 9:55:00 35786     
   //SSSSS表示5位秒數(shù)    
 

18.一年的第幾天

    select TO_CHAR(SYSDATE,'DDD'),sysdate from dual   
   //310  2002-11-6 10:03:51    
 

19.計(jì)算小時(shí),分,秒,毫秒

     SELECT
      Days,
      A,
      TRUNC (A * 24) Hours,
      TRUNC (A * 24 * 60 - 60 * TRUNC(A * 24)) Minutes,
      TRUNC (
        A * 24 * 60 * 60 - 60 * TRUNC (A * 24 * 60)
      ) Seconds,
      TRUNC (
        A * 24 * 60 * 60 * 100 - 100 * TRUNC (A * 24 * 60 * 60)
      ) mSeconds
    FROM
      (
        SELECT
          TRUNC (SYSDATE) Days,
          SYSDATE - TRUNC (SYSDATE) A
        FROM
          dual
      ) SELECT
        *
      FROM
        tabname
      ORDER BY
        DECODE (MODE, 'FIFO', 1 ,- 1) * TO_CHAR (rq, 'yyyymmddhh24miss')
      
   //   floor((date2-date1) /365) 作為年     
   //  floor((date2-date1, 365) /30) 作為月     
   //  d(mod(date2-date1, 365), 30)作為日.
 

20.next_day函數(shù)

   //返回下個(gè)星期的日期,day為1-7或星期日-星期六,1表示星期日
   next_day(sysdate,6)是從當(dāng)前開始下一個(gè)星期五。后面的數(shù)字是從星期日開始算起。     
   // 1  2  3  4  5  6  7     
   //日 一 二 三 四 五 六   
   select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from dual
   //日期 返回的是天 然后 轉(zhuǎn)換為ss

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

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

22,trunc[截?cái)嗟阶罱咏娜掌?單位為天] ,返回的是日期類型

   select sysdate S1,                    
     trunc(sysdate) S2,                 //返回當(dāng)前日期,無時(shí)分秒
     trunc(sysdate,'year') YEAR,        //返回當(dāng)前年的1月1日,無時(shí)分秒
     trunc(sysdate,'month') MONTH ,     //返回當(dāng)前月的1日,無時(shí)分秒
     trunc(sysdate,'day') DAY           //返回當(dāng)前星期的星期天,無時(shí)分秒
   from dual

23,返回日期列表中最晚日期

   select greatest('01-1月-04','04-1月-04','10-2月-04') from dual

24.計(jì)算時(shí)間差

     注:oracle時(shí)間差是以天數(shù)為單位,所以換算成年月,日
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual        //時(shí)間差-年
      select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual           //時(shí)間差-月
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual             //時(shí)間差-天
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual         //時(shí)間差-時(shí)
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual    //時(shí)間差-分
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //時(shí)間差-秒
 

25.更新時(shí)間

 //oracle時(shí)間加減是以天數(shù)為單位,設(shè)改變量為n,所以換算成年月,日
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
    to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from dual        //改變時(shí)間-年
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     add_months(sysdate,n) as newTime from dual                                 //改變時(shí)間-月
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual            //改變時(shí)間-日
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from dual         //改變時(shí)間-時(shí)
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual      //改變時(shí)間-分
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual   //改變時(shí)間-秒
 

26.查找月的第一天,最后一天

     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;

到此這篇關(guān)于Oracle之TO_DATE用法詳解的文章就介紹到這了,更多相關(guān)Oracle之TO_DATE內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Oracle刪除歸檔日志及添加定時(shí)任務(wù)

    Oracle刪除歸檔日志及添加定時(shí)任務(wù)

    當(dāng)ORACLE 歸檔日志滿了后,將無法正常登入ORACLE,需要?jiǎng)h除一部分歸檔日志才能正常登入ORACLE,下面這篇文章主要給大家介紹了關(guān)于Oracle刪除歸檔日志及添加定時(shí)任務(wù)的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • win10下oracle 11g安裝圖文教程

    win10下oracle 11g安裝圖文教程

    這篇文章主要為大家詳細(xì)介紹了win10下oracle11g安裝圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • oracle 提示登錄密碼過期完美解決方法

    oracle 提示登錄密碼過期完美解決方法

    這篇文章主要介紹了oracle 提示登錄密碼過期完美解決方法,在文中給大家補(bǔ)充介紹了Oracle使用scott用戶登錄提示密碼過期問題,需要的朋友參考下
    2018-04-04
  • Oracle創(chuàng)建和管理分區(qū)索引的操作方法

    Oracle創(chuàng)建和管理分區(qū)索引的操作方法

    創(chuàng)建和管理分區(qū)索引(Partitioned Index)是數(shù)據(jù)庫(kù)管理中的重要任務(wù),特別是在處理大規(guī)模數(shù)據(jù)集時(shí),以下是如何在Oracle數(shù)據(jù)庫(kù)中創(chuàng)建和管理分區(qū)索引的詳細(xì)步驟和示例代碼,感興趣的朋友一起看看吧
    2024-08-08
  • oracle通過1條語(yǔ)句插入多個(gè)值的方法示例

    oracle通過1條語(yǔ)句插入多個(gè)值的方法示例

    這篇文章主要給大家介紹了關(guān)于oracle通過1條語(yǔ)句插入多個(gè)值的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用oracle具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • ORACLE常見錯(cuò)誤代碼的分析與解決(二)

    ORACLE常見錯(cuò)誤代碼的分析與解決(二)

    ORACLE常見錯(cuò)誤代碼的分析與解決(二)...
    2007-03-03
  • Oracle使用MyBatis中RowBounds實(shí)現(xiàn)分頁(yè)查詢功能

    Oracle使用MyBatis中RowBounds實(shí)現(xiàn)分頁(yè)查詢功能

    這篇文章主要介紹了Oracle使用MyBatis中RowBounds實(shí)現(xiàn)分頁(yè)查詢 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Oracle中instr函數(shù)與substr函數(shù)及自制分割函數(shù)詳解

    Oracle中instr函數(shù)與substr函數(shù)及自制分割函數(shù)詳解

    這篇文章主要介紹了Oracle中instr函數(shù)與substr函數(shù)以及自制分割函數(shù),大家都知道substr函數(shù)就是很簡(jiǎn)單明了,就是個(gè)截取字符函數(shù),本文通過實(shí)例代碼對(duì)這接個(gè)函數(shù)詳細(xì)介紹,需要的朋友可以參考下
    2023-07-07
  • PLSQL配置遠(yuǎn)程Oracle數(shù)據(jù)庫(kù)連接的示例代碼

    PLSQL配置遠(yuǎn)程Oracle數(shù)據(jù)庫(kù)連接的示例代碼

    這篇文章主要介紹了PLSQL配置遠(yuǎn)程Oracle數(shù)據(jù)庫(kù)連接的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • ORACEL使用腳本來修改表結(jié)構(gòu)

    ORACEL使用腳本來修改表結(jié)構(gòu)

    修改表結(jié)構(gòu)在某些時(shí)候還是需要用到的,下面為大家介紹下使用腳本來修改表結(jié)構(gòu),感興趣的朋友不要錯(cuò)過
    2013-12-12

最新評(píng)論