Oracle數(shù)據(jù)庫中時間處理
Oracle數(shù)據(jù)庫中時間的處理:
4824小時的形式顯示出來要用HH24
select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;
select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual;
to_date() function
1.日期格式參數(shù) 含義說明
D 一周中的星期幾
DAY 天的名字,使用空格填充到9個字符
DD 月中的第幾天
DDD 年中的第幾天
DY 天的簡寫名
IW ISO標(biāo)準(zhǔn)的年中的第幾周
IYYY ISO標(biāo)準(zhǔn)的四位年份
YYYY 四位年份
YYY,YY,Y 年份的最后三位,兩位,一位
HH 小時,按12小時計
HH24 小時,按24小時計
MI 分
SS 秒
MM 月
Mon 月份的簡寫
Month 月份的全名
W 該月的第幾個星期
WW 年中的第幾個星期 1.日期時間間隔操作
當(dāng)前時間減去7分鐘的時間
select sysdate,sysdate - interval '7' MINUTE from dual
當(dāng)前時間減去7小時的時間
select sysdate - interval '7' hour from dual
當(dāng)前時間減去7天的時間
select sysdate - interval '7' day from dual
當(dāng)前時間減去7月的時間
select sysdate,sysdate - interval '7' month from dual
當(dāng)前時間減去7年的時間
select sysdate,sysdate - interval '7' year from dual
時間間隔乘以一個數(shù)字
select sysdate,sysdate - 8 *interval '2' hour from dual
2.日期到字符操作
select sysdate,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual
select sysdate,to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual
select sysdate,to_char(sysdate,'yyyy-ddd hh:mi:ss') from dual
select sysdate,to_char(sysdate,'yyyy-mm iw-d hh:mi:ss') from dual
參考o(jì)racle的相關(guān)關(guān)文檔(ORACLE901DOC/SERVER.901/A90125/SQL_ELEMENTS4.HTM#48515)
3. 字符到日期操作
select to_date('2003-10-17 21:15:37','yyyy-mm-dd hh24:mi:ss') from dual
具體用法和上面的to_char差不多。
4. trunk/ ROUND函數(shù)的使用
select trunc(sysdate ,'YEAR') from dual
select trunc(sysdate ) from dual
select to_char(trunc(sysdate ,'YYYY'),'YYYY') from dual
5.oracle有毫秒級的數(shù)據(jù)類型
--返回當(dāng)前時間 年月日小時分秒毫秒
select to_char(current_timestamp(5),'DD-MON-YYYY HH24:MI:SSxFF') from dual;
--返回當(dāng)前 時間的秒毫秒,可以指定秒后面的精度(最大=9)
select to_char(current_timestamp(9),'MI:SSxFF') from dual;
6.計算程序運行的時間(ms)
declare
type rc is ref cursor;
l_rc rc;
l_dummy all_objects.object_name%type;
l_start number default dbms_utility.get_time;
begin
for I in 1 .. 1000
loop
open l_rc for
'select object_name from all_objects '||
'where object_id = ' || i;
fetch l_rc into l_dummy;
close l_rc;
end loop;
dbms_output.put_line
( round( (dbms_utility.get_time-l_start)/100, 2 ) ||
' seconds...' );
end;
to_char() function
The following are number examples for the to_char function.
to_char(1210.73, '9999.9') would return '1210.7'
to_char(1210.73, '9,999.99') would return '1,210.73'
to_char(1210.73, '$9,999.00') would return '$1,210.73'
to_char(21, '000099') would return '000021'
The following is a list of valid parameters when the to_char function is used to convert a date to a string. These parameters can be used in many combinations.
Parameter Explanation
YEAR Year, spelled out
YYYY 4-digit year
YYY
YY
Y Last 3, 2, or 1 digit(s) of year.
IYY
IY
I Last 3, 2, or 1 digit(s) of ISO year.
IYYY 4-digit year based on the ISO standard
關(guān)于Oracle數(shù)據(jù)庫中時間的處理就為大家講解到這里,可能還不是很全面,以后有機會還會繼續(xù)為大家講解這方面的知識,希望對大家能夠有所幫助。