带时分秒 转换成日期类型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 系统当期那日期和时间
==================================================================
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/