建立實例數據表
建立一張訂單表 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;