Home > Database > Mysql Tutorial > oracle日期时间型timestamp相关转换

oracle日期时间型timestamp相关转换

PHPz
Release: 2018-09-30 09:13:47
Original
2824 people have browsed it

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数据类型。

更多相关教程请访问   MySQL视频教程

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template