PLSQL日期函数及Trunc函数的使用

带时分秒 转换成日期类型to_date('20120801 00:00:00','yyyymmdd HH24:Mi:SS')
六种日期函数:
1. add_months(日期,number) 指定日期推迟number个月
2. last_day(日期) 指定日期当月的最后一天
3. new_time(日期,时区简写) 调整时区
4. next_day(日期,number) number表示周几,星期日是1,指定number的日期(一周内或一周后)
5. months_between(日期1,日期2) 日期1和日期2之间有几个月
6. sysdate 系统当期那日期和时间

==================================================================

1.TRUNC ---- for dates

  其具体的语法格式如下:

  TRUNC(date[,fmt])返回日期d1所在期间(参数c1)的第一天日期

  其中:

  date:日期时间类型,将要格式化的日期

  fmt:字符串型,日期的格式,默认是j(即当前日期),返回的日期所在期间由fmt指定的第一天。忽略它则返回年月日的形式。

             fmt对应的参数表:

             本周星期日:day或dy或d (每周顺序:日,一,二,三,四,五,六)

             本季日期:q

             本年初日期:syear或year或yyyy或yyy或yy或y(多个y表示精度) 

             本月初日期:month或mon或mm或rm

             本日的日期:dd

             本日期的小时数:hh或HH24

             本日期的分钟数:mi或Mi

             本世纪初日期:cc或scc

  下面是该函数的使用情况:

select sysdate from dual; --当前日期和时间
select trunc(sysdate) from dual;--今天日期,等同于trunc(sysdate ,'dd')
select trunc(sysdate ,'dd') from dual; --今天日期,默认值
select trunc(sysdate,'d')+7 from dual;--本周星期日的日期
select trunc(sysdate,'dy')+7 from dual;  --本周星期日的日期
select trunc(sysdate,'day')+7 from dual; --本周星期日的日期
select trunc(sysdate,'q') from dual; --本季开始日期
select trunc(sysdate,'month') from dual; --本月开始日期
select trunc(sysdate ,'mm') from dual; --本月开始日期
select trunc(sysdate,'year') from dual;  --本年开始日期
select trunc(sysdate ,'yyyy') from dual; --本年开始日期
select trunc(sysdate ,'HH24') from dual; --本小时开始时间
select trunc(sysdate ,'MI') from dual; --本分钟开始时间
select trunc(sysdate ,'CC') from dual; --本世纪开始日期
select trunc(LAST_DAY(sysdate),'dd') from dual; --本月最后一天
select trunc(sysdate)+8/24 from dual;  --取当天早上8点的时间
select trunc(sysdate,'HH')+8/24 from dual; -- 当前时间精确到小时,再往后推8小时

 

2.TRUNC ---- for number

  TRUNC函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定小数点位数只后的部分做相应舍入选择处理,而统统截去。

  其具体的语法格式如下

  TRUNC(number[,decimals])

  其中:

  number 待做截取处理的数值

  decimals 指明需保留小数点后面或前面的位数。可选项,忽略它则截去所有的小数部分

  下面是该函数的使用情况:

  TRUNC(89.985,2)=89.98

  TRUNC(89.985)=89

  TRUNC(89.985,-1)=80

  注意:第二个参数可以为负数,表示为小数点左边指定位数后面的部分截去,即均以0记。与取整类似,比如参数为1即取整到十分位,如果是-1,则是取整到十位,以此类推。 

 下面是该函数对数字的用法:

select trunc(123.458) from dual; --123
select trunc(123.458,0) from dual; --123
select trunc(123.458,1) from dual; --123.4
select trunc(123.458,-1) from dual; --120
select trunc(123.458,-4) from dual; --0
select trunc(123.458,5) from dual;  --123.458
select trunc(123) from dual;  --123
select trunc(123,1) from dual; --123
select trunc(123,-1) from dual; --120

 

3.trunc()函数与round()函数的比较

round()函数可以对数字按指定保留小数位数四舍五入,这个函数还可以对日期四舍五入
round函数的数字的用法
select round(123.458) from dual; --123
select round(123.458,0) from dual; --123
select round(123.458,1) from dual; --123.5
select round(123.458,-1) from dual; --120
select round(123.458,-4) from dual; --0
select round(123.458,5) from dual;  --123.458
select round(123) from dual;  --123
select round(123,1) from dual; --123
select round(123,-1) from dual; --120
select round(51.458,-2) from dual; --100

round函数的日期时间的用法

select round(sysdate) from dual;--四舍五入到日
select round(sysdate,'yyyy') from dual; --四舍五入到年
select round(sysdate,'mm') from dual; --四舍五入到月
select round(sysdate,'dd') from dual; --四舍五入到日
select round(sysdate,'hh') from dual; --四舍五入到小时
select round(sysdate,'mi') from dual; --四舍五入到分钟
select round(sysdate,'d') from dual; --四舍五入到周,星期日的日期
select round(sysdate,'q') from dual; --四舍五入到季的开始日期
select round(sysdate ,'CC') from dual; --四舍五入到世纪开始日期

select round(to_date('20161215 00:00:00','yyyymmdd HH24:Mi:SS') ,'d') from dual;--四舍五入到周,星期日的日期

 

参考出处:http://blog.csdn.net/eleven204/article/details/6712538

http://www.cnblogs.com/gengaixue/archive/2012/11/21/2781037.html

==================================================================
oracle plsql 对日期的处理函数和sql例子

102.取时间点的年份的写法:
select to_char(sysdate,'yyyy') from dual;

103.取时间点的月份的写法:
select to_char(sysdate,'mm') from dual;

104.取时间点的日的写法:
select to_char(sysdate,'dd') from dual;

105.取时间点的时的写法:
select to_char(sysdate,'hh24') from dual;

106.取时间点的分的写法:
select to_char(sysdate,'mi') from dual;

107.取时间点的秒的写法:
select to_char(sysdate,'ss') from dual;

108.取时间点的日期的写法:
select trunc(sysdate) from dual;

109.取时间点的时间的写法:
select to_char(sysdate,'hh24:mi:ss') from dual;

110.日期,时间形态变为字符形态
select to_char(sysdate) from dual;

111.将字符串转换成日期或时间形态:
select to_date('2003/08/01') from dual;

112.返回参数的星期几的写法:
select to_char(sysdate,'d') from dual;

113.返回参数一年中的第几天的写法:
select to_char(sysdate,'ddd') from dual;

114.返回午夜和参数中指定的时间值之间的秒数的写法:
select to_char(sysdate,'sssss') from dual;

115.返回参数中一年的第几周的写法:
select to_char(sysdate,'ww') from dual;
==================================================================
练习时的代码:
select to_number(to_char(last_day(add_months(to_date('20040406','yyyymmdd'),-1))+1,'yyyymmdd')) from dual;
------------------------------
20040401

select to_number(to_char(last_day(to_date('20040406','yyyymmdd')),'yyyymmdd')) from dual;
------------------------------
20040430

--select sysdate from dual; 当前日期
--select last_day(sysdate) from dual; 月底日期
--select last_day(add_months(sysdate, -1)) from dual; 上月底日期
-- SELECT to_char(last_day(SYSDATE),'dd') days FROM dual; 当前月的天数
--select last_day(add_months(sysdate,-1))+1 from dual; 当前月第一天
--select to_number(to_char(sysdate,'yyyymmdd')) from dual;系统当前日期转换成如20070910格式:

==================================================================

create or replace procedure p_hkb_date_insert is
/*
过程功能描述:日期插入表中
*/
v_days number(10);
v_date date;
i number(10);
begin
  begin
    --取得当月天数
    select to_number(to_char(last_day(sysdate), 'dd')) into v_days from dual;
  end;

  i := 1;
  begin
    select last_day(add_months(sysdate, -1)) into v_date from dual;
    while i <= v_days loop
      insert into hkb_date values(v_date + i, to_char(v_date + i, 'yyyymmdd'),to_number(to_char(v_date + i, 'yyyymmdd')));
      i := i + 1;
    end loop;
  end;
end p_hkb_date_insert;
==================================================================
create table hkb_date_construct as select * from hkb_date where 1=2; 继承表字段
create table hkb_date_data as select * from hkb_date; 继承表记录

出处:http://geniuszhe.blog.163.com/blog/static/11934682012731574991/

        http://namelysweet.blog.hexun.com/5714847_d.html

posted on 2012-11-09 16:41  jack_Meng  阅读(78375)  评论(0编辑  收藏  举报

导航