--格式化输出毫秒,后面的‘3’指定输出精确度
--9i以上版本
SELECT to_char(systimestamp,'YYYY-MM-DD HH24:MI:SS.FF3') FROM DUAL;
/*
值得一提的是:毫秒的显示精度是6位,不过有效位是3位,即最大值达到999,满1000ms就进为1s
date类型只能到秒
用timestamp可以到毫秒
*/
select to_char(systimestamp,'YYYY-MM-DD HH24:MI:SS.FF') from dual
select to_char(sysdate,'sssssss') from dual;
9i可以用select systimestamp from dual;
在8i或更高版本中可以用dbms_utility.get_time,或select * from v$timer;
在9i中要得到精确到毫秒的时间直接用“TimeStamp”类型变量:
declare
cur_time timestamp;
begin
cur_time:=current_timestamp;
dbms_output.put_line(to_char(cur_time));
end;
8i到毫秒
SQL>create or replace and compile
java source
named "MyTimestamp"
as
import java.lang.String;
import java.sql.Timestamp;
public class MyTimestamp
{
public static String getTimestamp()
{
return(new Timestamp(System.currentTimeMillis())).toString();
}
};
SQL>java created.
注:注意java的语法,注意大小写
SQL>create or replace function my_timestamp return varchar2
as language java
name 'MyTimestamp.getTimestamp() return java.lang.String';
/
SQL>function created.
SQL>select my_timestamp,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') ORACLE_TIME from dual;
MY_TIMESTAMP ORACLE_TIME
------------------------ -------------------
2003-03-17 19:15:59.688 2003-03-17 19:15:59
dbms_utility.get_time获取的是相对时间
SYSTIMESTAMP可以获取到毫秒。
TIMESTAMP转成CHAR的格式可以为
select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ssxff') from dual;
SQL> select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss ff3') from dual;
TO_CHAR(SYSTIMESTAMP,'YYYY-MM-
------------------------------
2003-06-11 10:46:17 046
Executed in 0 seconds
Oracle 时间戳
TIMESTAMP WITH LOCAL TIME ZONE 类型对TIMESTAMP 进行了扩展,用于存储一个转换为数据库设置的本地时区的时间戳。在为一个TIMESTAMP WITH LOCAL TIME ZONE 类型的列存储一个时间戳值时,这个时间戳就被转换(或称为被规格化)为数据库的时区设置。此后再对这个时间戳进行检索,它就被规格化为当前会话中的时区设置。
提示:
如果一个企业已经实现了一个全球系统,可以通过世界各地进行访问,那么 TIMESTAMP WITH LOCAL TIME ZONE 类型就会非常有用。这是因为数据库会将时间戳存储为数据库所在时区中的本地时间,但是在对它们进行检索时,却可以看到规格化为本地时区的时间戳。