唐僧还在拜佛求经路。  

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;

posted on 2017-10-04 10:26  唐僧还在拜佛求经路。  阅读(1398)  评论(0编辑  收藏  举报