您好,欢迎访问一九零五行业门户网

oracle日期相加减,tochar(),todata()

1、获得时间差毫秒数: select ceil((to_date('2008-05-02 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - to_date('2008-04-30 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60 * 1000) 相差豪秒数 from dual; 相差豪秒数 ---------- 86401000 2、获得相差
1、获得时间差毫秒数:
select ceil((to_date('2008-05-02 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - to_date('2008-04-30 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60 * 1000) 相差豪秒数 from dual;
相差豪秒数
----------
  86401000
2、获得相差秒数:
select ceil((to_date('2008-05-02 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - to_date('2008-04-30 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60) 相差秒数 from dual;
相差秒数
----------
     86401
3、获得相差分钟数、小时数,以此类推,默认时间相减获得相差的天数
4、获得相差的月份数
select (extract(year from to_date('2009-05-01','yyyy-mm-dd')) - extract(year from to_date('2008-04-30','yyyy-mm-dd'))) * 12 +
       extract(month from to_date('2008-05-01','yyyy-mm-dd')) - extract(month from to_date('2008-04-30','yyyy-mm-dd')) months
from dual;
months
----------
        13
这里可以直接使用months_between函数
5、获得相差的年份
select extract(year from to_date('2009-05-01','yyyy-mm-dd')) - extract(year from to_date('2008-04-30','yyyy-mm-dd')) years from dual;
years
----------
         1
6、获得时间的方法:
select sysdate,add_months(sysdate,12) from dual;  --加1年
select sysdate,add_months(sysdate,1) from dual;   --加1月
select sysdate,to_char(sysdate+7,'yyyy-mm-dd hh24:mi:ss') from dual;  --加1星期
select sysdate,to_char(sysdate+1,'yyyy-mm-dd hh24:mi:ss') from dual;  --加1天
select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd hh24:mi:ss') from dual;  --加1小时
select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd hh23:mi:ss') from dual;  --加1分钟
select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd hh23:mi:ss') from dual;  --加1秒
select   sysdate+7   from   dual;                     --加7天
将当前日期转换为上一个月
  select to_char(add_months(sysdate, -1), 'yyyymm') --获取当前时间的前一个月
            from dual;
select sysdate from dual; /**获取当前时间到秒**/
select  sysdate-3 from dual;/**获取当前2天**/
select round(sysdate)  as 格式成日期 from dual;
select to_date('2008-9-2','yyyy_mm_dd') as 格式成日期 from dual;
select to_char(add_months(sysdate, -1), 'yyyymm') from dual; /**--获取当前时间的前一个月,正向后**/
select last_day(sysdate) from dual;/**本月最受一天**/
/***分别取时间的年、月、日***/
select to_char(sysdate,'yyyy') from dual;
select to_char(sysdate,'mm') from dual;
select to_char(sysdate,'dd') from dual;
1。上月末天:
sql> select to_char(add_months(last_day(sysdate),-1),'yyyy-mm-dd') lastday from
dual;
lastday
----------
2005-05-31
2。上月今天
sql> select to_char(add_months(sysdate,-1),'yyyy-mm-dd') pretoday from dual;
pretoday
----------
2005-05-21
3.上月首天
sql> select to_char(add_months(last_day(sysdate)+1,-2),'yyyy-mm-dd') firstday from dual;
firstday
----------
2005-05-01
4.按照每周进行统计
sql> select to_char(sysdate,'ww') from dual group by to_char(sysdate,'ww');
to
--
25
5。按照每月进行统计
sql> select to_char(sysdate,'mm') from dual group by to_char(sysdate,'mm');
to
--
06
6。按照每季度进行统计
sql> select to_char(sysdate,'q') from dual group by to_char(sysdate,'q');
t
-
2
7。按照每年进行统计
sql> select to_char(sysdate,'yyyy') from dual group by to_char(sysdate,'yyyy');
to_c
----
2005
8.要找到某月中所有周五的具体日期
select to_char(t.d,'yy-mm-dd') from (
select trunc(sysdate, 'mm')+rownum-1 as d
from dba_objects
where rownum where to_char(t.d, 'mm') = to_char(sysdate, 'mm') --找出当前月份的周五的日期
and trim(to_char(t.d, 'day')) = '星期五'
--------
03-05-02
03-05-09
03-05-16
03-05-23
03-05-30
如果把where to_char(t.d, 'mm') = to_char(sysdate, 'mm')改成sysdate-90,即为查找当前月份的前三个月中的每周五的日期。
9.oracle中时间运算
内容如下:
1、oracle支持对日期进行运算
2、日期运算时是以天为单位进行的
3、当需要以分秒等更小的单位算值时,按时间进制进行转换即可
4、进行时间进制转换时注意加括号,否则会出问题
sql> alter session set nls_date_format='yyyy-mm-dd hh:mi:ss';
会话已更改。
sql> set serverout on
sql> declare
  2 datevalue date;
  3 begin
  4 select sysdate into datevalue from dual;
  5 dbms_output.put_line('源时间:'||to_char(datevalue));
  6 dbms_output.put_line('源时间减1天:'||to_char(datevalue-1));
  7 dbms_output.put_line('源时间减1天1小时:'||to_char(datevalue-1-1/24));
  8 dbms_output.put_line('源时间减1天1小时1分:'||to_char(datevalue-1-1/24-1/(24*60)));
  9 dbms_output.put_line('源时间减1天1小时1分1秒:'||to_char(datevalue-1-1/24-1/(24*60)-1/(24*60*60)));
10 end;
11 /
源时间:2003-12-29 11:53:41
源时间减1天:2003-12-28 11:53:41
源时间减1天1小时:2003-12-28 10:53:41
源时间减1天1小时1分:2003-12-28 10:52:41
源时间减1天1小时1分1秒:2003-12-28 10:52:40
pl/sql 过程已成功完成。
在oracle中实现时间相加处理
-- 名称:add_times
-- 功能:返回d1与newtime相加以后的结果,实现时间的相加
-- 说明:对于newtime中的日期不予考虑
-- 日期:2004-12-07
-- 版本:1.0
-- 作者:kevin
create or replace function add_times(d1 in date,newtime in date) return date
is
  hh   number;
  mm   number;
  ss   number;
  hours number;
  dresult  date; 
begin
  -- 下面依次取出时、分、秒
  select to_number(to_char(newtime,'hh24')) into hh from dual;
  select to_number(to_char(newtime,'mi')) into mm from dual;
  select to_number(to_char(newtime,'ss')) into ss from dual;
  -- 换算出newtime中小时总和,在一天的百分几
  hours := (hh + (mm / 60) + (ss / 3600))/ 24;
  -- 得出时间相加后的结果
  select d1 + hours into dresult from dual;
  return(dresult);
end add_times;
-- 测试用例
-- select add_times(sysdate,to_date('2004-12-06 03:23:00','yyyy-mm-dd hh24:mi:ss')) from dual
在oracle9i中计算时间差
计算时间差是oracle data数据类型的一个常见问题。oracle支持日期计算,你可以创建诸如“日期1-日期2”这样的表达式来计算这两个日期之间的时间差。
一旦你发现了时间差异,你可以使用简单的技巧来以天、小时、分钟或者秒为单位来计算时间差。为了得到数据差,你必须选择合适的时间度量单位,这样就可以进行数据格式隐藏。
使用完善复杂的转换函数来转换日期是一个诱惑,但是你会发现这不是最好的解决方法。
round(to_number(end-date-start_date))- 消逝的时间(以天为单位)
round(to_number(end-date-start_date)*24)- 消逝的时间(以小时为单位)
round(to_number(end-date-start_date)*1440)- 消逝的时间(以分钟为单位)
显示时间差的默认模式是什么?为了找到这个问题的答案,让我们进行一个简单的sql *plus查询。
sql> select sysdate-(sysdate-3) from dual;
sysdate-(sysdate-3)
-------------------
                   3
这里,我们看到了oracle使用天来作为消逝时间的单位,所以我们可以很容易的使用转换函数来把它转换成小时或者分钟。然而,当分钟数不是一个整数时,我们就会遇到放置小数点的问题。
select
    (sysdate-(sysdate-3.111))*1440
from
    dual;
(sysdate-(sysdate-3.111))*1440
------------------------------
                     4479.83333
当然,我们可以用round函数(即取整函数)来解决这个问题,但是要记住我们必须首先把date数据类型转换成number数据类型。
select
    round(to_number(sysdate-(sysdate-3.111))*1440)
from
    dual;
round(to_number(sysdate-(sysdate-3.111))*1440)
----------------------------------------------
                                           4480
我们可以用这些函数把一个消逝时间近似转换成分钟并把这个值写入oracle表格中。在这个例子里,我们有一个离线(logoff)系统级触发机制来计算已经开始的会话时间并把它放入一个oracle statspack user_log扩展表格之中。
update
    perfstat.stats$user_log
set
    elapsed_minutes =
    round(to_number(logoff_time-logon_time)*1440)
where
    user = user_id
and
    elapsed_minutes is null;
查出任一年月所含的工作日
create or replace function get_workingdays(
  ny in varchar2
) return integer is
/*------------------------------------------------------------------------------------------
函数名称:get_workingdays
中文名称:求某一年月中共有多少工作日
作者姓名: xingping
编写时间: 2004-05-22
输入参数:ny:所求包含工作日数的年月,格式为yyyymm,如200405
返 回 值:整型值,包含的工作日数目。
算法描述:
    1).列举出参数给出的年月中的每一天。这里使用了一个表(ljrq是我的库中的一张表。这个表可以是有权访问的、记录条数至少为31的任意一张表或视图)来构造出某年月的每一天。
    2).用这些日期和一个已知星期几的日期相减(2001-12-30是星期天),所得的差再对7求模。如果所求年月在2001-12-30以前,那么所得的差既是负数,求模后所得值范围为大于-6,小于0,如-1表示星期六,故先将求模的结果加7,再求7的模.
    3).过滤掉结果集中值为0和6的元素,然后求count,所得即为工作日数目。     
-------------------------------------------------------------------------------------------------*/
  result integer;
begin
  select count(*) into result
    from (select mod(mod(q.rq-to_date('2001-12-30','yyyy-mm-dd'),7),7) weekday
            from ( select to_date(ny||t.dd,'yyyymmdd') rq
                     from (select substr(100+rownum,2,2) dd
                             from ljrq z where rownum                          ) t
                     where to_date(ny||t.dd,'yyyymmdd')
                       between to_date(ny,'yyyymm')
                           and last_day(to_date(ny,'yyyymm'))
                 )q
         ) a  
    where a.weekday not in(0,6);   
  return result; 
end get_workingdays;
______________________________________
还有一个版本
create or replace function get_workingdays(
  ny in varchar2
) return integer is
/*-----------------------------------------------------------------------------------------
函数名称:get_workingdays
中文名称:求某一年月中共有多少工作日
作者姓名: xingping
编写时间: 2004-05-23
输入参数:ny:所求包含工作日数的年月,格式为yyyymm,如200405
返 回 值:整型值,包含的工作日数目。
算法描述:使用last_day函数计算出参数所给年月共包含多少天,根据这个值来构造一个循环。在这个循环中先求这个月的每一天与一个已知是星期天的日期(2001-12-30是星期天)的差,所得的差再对7求模。如果所求日期在2001-12-30以前,那么所得的差既是负数,求模后所得值范围为大于-6,小于0,如-1表示星期六,故先将求模的结果加7,再求7的模. 如过所得值不等于0和6(即不是星期六和星期天),则算一个工作日。     
----------------------------------------------------------------------------------------*/
  result integer := 0;
  myts integer;      --所给年月的天数
  scts integer;      --某天距2001-12-30所差的天数
  rq   date;
  djt integer := 1;   --
begin
  myts := to_char(last_day(to_date(ny,'yyyymm')),'dd'); 
  loop
    rq := to_date(ny||substr(100+djt,2),'yyyymmdd');
    scts := rq - to_date('2001-12-30','yyyy-mm-dd');
    if mod(mod(scts,7)+7,7) not in(0,6) then
      result := result + 1;
    end if;
    djt := djt + 1; 
    exit when djt>myts;
  end loop; 
  return result; 
end get_workingdays;
以上两个版本的比较
第一个版本一条sql语句就可以得出结果,不需要编程就可以达到目的。但需要使用任意一张有权访问的、记录条数至少为31的一张表或视图。
    第二个版本需要编程,但不需要表或者视图。
    这两个版本都还存在需要完善的地方,即没有考虑节日,如五一、十一、元旦、春节这些节假期都没有去除。这些节假日应该维护成一张表,然后通过查表来去除这些节假日。
其它类似信息

推荐信息