数据库之日期类型

建立实例数据表

建立一张订单表   myorder1111

表子段:

id  number primary  key(值不能重,不能是空)
name  varchar2(30)
money  number
odate  date

 

向表中插入数据

1 ,‘jd0001’,123.45 ,sysdate

       2 ,‘jd0002’,345.85 ,sysdate

提交数据(commit)

  

 

把id等于2的订单钱数改为2345.68。

日期类型简介

(1)日期类型默认的表现形式

    ‘dd - MON - yy’

(2)如何改变默认的表现

to_char ( par1 , par2 ) 
par1   要处理的日期数据或者日期字段
par2   是日期格式

日期格式:

  • yyyy     4位年
  • mm      2位月
  • dd       2位天
  • hh       12小时制
  • hh24   24小时制
  • mi      分钟
  • ss      秒
  • day       星期几
  • mon     月的缩写
  • month  月的全写

  演示:按照入职日期排序,显示s_emp表中id  salary start_date 。

select id, salary, start_date from s_emp order by start_date;

。。。。。。

 

用to_char改过的格式可以避免一些误会:

select id, salary, to_char(start_date, 'yyyy-mm-dd hh24:mi:ss') start_date from s_emp order by start_date;

  。。。。。。

      查看s_emp的脚本可得只有id=1的时间是to_date放入了年月日时分秒,其他的都是默认放入日期(08-MAR-90),时分秒默认0。

放入任意的时间点到数据库

to_date(par1,par2) /*把日期字符串变成日期*/
  • par1  日期字符串  ‘2008-08-08  20:08:08’
  • par2  日期格式字符串,和to_char一样

演示:

insert into myorder values(2008, 'bj00001', 200000,to_date('2008-08-08 20:08:08', 'yyyy-mm-dd hh24:mi:ss'));
select id, to_char(odate, 'yyyy-mm-dd hh24:mi:ss') odate from myorder where id=2008;

 

insert into myorder values(2012, 'chuan_piao', 888888,to_date('2012-12-22 23:59:59', 'yyyy-mm-dd hh24:mi:ss'));
select id, to_char(odate, 'yyy-mm=dd hh24:mo:ss');

日期的调整

表现当前系统日期:

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;

 

调整一天:加1

select to_char(sysdate+1, 'yyyy-mm-dd hh24:mi:ss') from dual;

 

 

调整一个小时:加1/24

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(sysdate+1/24, 'yyyy-mm-dd hh24:mi:ss') from dual;

 

 

按照分钟按照秒为单位调整以此类推:

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(sysdate+1/24(24*60*60), 'yyyy-mm-dd hh24:mi:ss') from dual;

 

特殊的调整

add_months(par1,par2)     按月为单位进行调整

 演示: 

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(add_mouths(sysdate, 1), 'yyyy-mm-dd hh24:mi:ss') from dual;

  

 

round(par1,par2)(用的少)
  • par1   要处理的日期或日期字段
  • par2   要四舍五入的单位  默认单位是天(半天以上入,半天以下舍)

‘hh’是小时为单位(半个小时以上就入,半个小时以下舍)

‘mm’月,‘yy’年

演示:

以天为单位:

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'), to_char(round(sysdate), 'yyyy-mm-dd hh24:mi:ss') from dual;

   

 

以小时为单位:

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'), to_char(round(sysdate), 'hh', 'yyyy-mm-dd hh24:mi:ss') from dual;

 

 

trunc(par1,par2)(用的少)
  • par1   要处理的日期或日期字段
  • par2   要截取的单位  默认单位是天(半天以上入,半天以下舍)

用法和round一样,只是不入,不管后面是多少都直接截掉

演示:以月为单位截取:(日期没有0,截取后日期变为1)

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(trunc(sysdate, 'mm'), 'yyyy-mm-dd hh24:mi:ss') from dual;

 

日期的综合处理

sysdate   得到这个日期对应的月的最后一天的最后一秒

8月应该得到:2014-08-31 23:59:59

‘2008-01-20  08:30:25’应该得到:2014-01-31  23:59:59

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(add)months(trunc(sysdate, 'mm '),1)-1/(24*60*60), 
  'yyyy-mm-dd hh24:mi:ss') from dual;

 

last_day(par1)得到日期对应的月的最后一天对应的时间点
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(trunc(last_day(sysdate))+1-1/(24*60*60), 'yyyy-mm-dd hh24:mi:ss') from dual;

 

 

next_day(par1,par2)下个星期几的时间点

08-25是星期一,下一个星期一是09-01

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(mext_day(sysdate, 'moneday'), 'yyyy-mm-dd hh24:mi:ss') from dual;

 

08-25是星期一,下一个星期五是08-29(离08-25最近的周五)

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(mext_day(sysdate,'friday'), 'yyyy-mm-dd hh24:mi:ss') from dual;

 

posted @ 2018-06-23 00:05  嵌动初心(aaron)  阅读(14254)  评论(0编辑  收藏  举报