oracle日期时间型Date和TimeStamp
ALTER SESSION SET NLS_DATE_FORMAT =\'YYYY-MM-DD HH24:MI:SS\';
ALTER SESSION SET NLS_TIMESTAMP_FORMAT=\'YYYY-MM-DD HH24:MI:SS:FF3\';
SELECT * FROM NLS_SESSION_PARAMETERS;
1、获取系统时间的语句(ssxff6获取小数点后面六位)
select sysdate,systimestamp,to_char(systimestamp, \'yyyymmdd hh24:mi:ssxff6\'),
to_char(systimestamp, \'yyyymmdd hh24:mi:ss.ff6\') from dual;
2、字符型转成timestamp
select to_timestamp(\'2011-09-14 12:52:42.123456789\', \'syyyy-mm-dd hh24:mi:ss.ff\') from dual;
3、timestamp转成date型
select cast(to_timestamp(\'2011-09-14 12:52:42.123456789\', \'syyyy-mm-dd hh24:mi:ss.ff\') as date) timestamp_to_date from dual;
4、date型转成timestamp
select cast(sysdate as timestamp) date_to_timestamp from dual;
5、两date的日期相减得出的是天数,而两timestamp的日期相减得出的是完整的年月日时分秒小数秒
select sysdate-sysdate,systimestamp-systimestamp from dual;
select extract(day from inter) * 24 * 60 * 60 +
extract(hour from inter) * 60 * 60 + extract(minute from inter) * 60 +
extract(second from inter) "seconds" from
(
select to_timestamp(\'2011-09-14 12:34:23.281000000\', \'yyyy-mm-dd hh24:mi:ss.ff\') -
to_timestamp(\'2011-09-14 12:34:22.984000000\', \'yyyy-mm-dd hh24:mi:ss.ff\') inter from dual
);
select extract(second from to_timestamp(\'2011-09-14 12:34:23.281000000\', \'yyyy-mm-dd hh24:mi:ss.ff\'))-
extract(second from to_timestamp(\'2011-09-14 12:34:22.984000000\', \'yyyy-mm-dd hh24:mi:ss.ff\')) from dual;
注:所以,timestamp要算出两日期间隔了多少秒,要用函数转换一下。
to_char函数支持date和timestamp,但是trunc却不支持TIMESTAMP数据类型。
先简单说下 WIndows下配置timestamp的显示格式:
添加一个系统环境变量:
名称: NLS_TIMESTAMP_FORMAT
值: YYYY-MM-DD HH24:MI:SS:FF6
保存后, 重启pl/sql developer 即可.
在数据库中直接插入的timestamp型的数据(2011-10-23) ,则在数据库默认为
2011-10-23 00:00:00
如果想查询2011-10-23号的数据
则有两种方法
第一种是 begintime < to_timestamp(\'2011-10-24\', \'yyyy-mm-dd hh24:mi:ss.ff\')
第二种 begintime <= to_timestamp(\'2011-10-24\', \'yyyy-mm-dd hh24:mi:ss.ff\') ++0.99999
从数据库中查询 select to_timestamp(\'2011-10-24\', \'yyyy-mm-dd hh24:mi:ss.ff\')+0.99999 from dual
看到结果为2011-10-24 下午 11:59:59
如果想查2011-10-23号中午之前截至的数据则
select to_timestamp(\'2011-10-24\', \'yyyy-mm-dd hh24:mi:ss.ff\')+0.5 from dual