oracle 日期函數

來源:互聯網
上載者:User

標籤:函數   to_date   sele   toc   指定   last   trunc   浮點型   where   

select  ename,hiredate,sysdate from scott.emp ;     可以查詢出員工入職到現在的日期
  select ename,sysdate-hiredate from scott.emp;           查看在職員工已經多少天;
  select ename,months_between(sysdate,hiredate) from scott.emp 多少月
  select add_months(sysdate,6) from scott.emp             系統時間加6個月的時間;

  number_tochar:
        select to_char(sysdate,‘YYYY-MM-DD‘)  from dual;
        select ename,to_char(sal,‘L99,999.99‘) from dual;
        select ename,to_char(sal,‘$00,000.00‘) from dual;
            9:代表一個數
            0:強制顯示一個零
            $:放置一個浮點型貨幣符號
            L:使用浮點型當地貨幣符號
  date_to_char:
  char_to_date:
        select to_date(‘1990-09-05‘,‘YYYY-MM-DD‘) from dual;
  char_to_number:
        select to_number(‘$00,800.00‘,‘L00,000.00‘) from dual; 800
---------
select
  to_char(sysdate,‘yyyy‘) cyrr_year
  to_char(to_date(‘07‘,‘yy‘),‘YYYY‘)  yy07,
  to_char(to_date(‘97‘,‘yy‘),‘YYYY‘)  yy97,
  to_char(to_date(‘07‘,‘rr‘),‘YYYY‘)  rr07,
  to_char(to_date(‘97‘,‘rr,‘YYYY‘)  rr97
  from dual;
------------
  實際上對於日期提供以下三種計算模式:
    日期+數字=日期(若干天之後的日期)
      select sysdate +10 from dual;
    日期-數字=日期(若干天之前的日期)
      select sysdate -10 from dual;
    日期-日期=數字(兩個日期期間的天數)
      計算每一位僱員到今天為止的僱傭天數。
        select ename,hiredate, sysdate-hiredate form scott.emp
計算兩個日期見所經曆的月數總和。
  文法:數字 MONTHS_BETWEEN(日期1,日期2)

      MONTHS_BETWEEN函數返回兩個日期之間的月份數。

      1:範例: 計算每一位員工到今天為止僱傭總年數。
         select ename,hiredate,
                  trunc(months_between(sysdate,hiredate)/12) years from scott.emp
      2:增加若干月之後的月日期;
          範例:測試ADD_MONTHS函數
              select add_months(sysdate,4) from dual;
      3:計算還要差1年滿34年的僱傭日期的全部僱傭。
              select * from scott.emp
                where trunc(months_between(sysdate,hiredate)/12)=34 ;
      4:計算指定日期所在月的最後一天
              select last_day(sysdate) from dual;
      5:查詢出所有僱傭所在月的倒數第二天被僱傭的僱員資訊
          每個僱員的僱傭日期是不一樣的,所有每一個僱傭日期所在月的倒數第二天也不一樣。
              select ename,hiredate,last_day(hiredate),last_day(hiredate)-2 from scott.emp;
      6:計算下一個周二
          select next_day(sysdate,‘星期二’) from dual;
      7:計算scott.emp表中僱傭的員工到目前為止的僱傭年份:
            select empno,ename,hiredate, trunc(months_between(sysdate,hiredate)/12) year from scott.emp
      8:綜合分析:要求查詢出僱員的編號、姓名、僱傭日期,以及每一位僱員到今天為止被僱傭的年數,月數天數。
             假設下載的日期是:2016-03-08.
             select empno,ename,hiredate,
                trunc(months_between(sysdate,hiredate)/12) gear,
                trunc(mod(months_between(sysdate,hiredate,)/12)) months,
                trunc(sysdate-add_months(hiredate,months_between(sysdate,hiredate))) day
                from scott.emp;

oracle 日期函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.