欢迎进入oracle社区论坛,与200万技术人员互动交流 >>进入
1 row created.
sql> select * from test_zone;
timestamp_dt zone_dt local_zone_dt
----------------------------------- ----------------------------------- -----------------------------------
06-sep-12 01.50.10.000000 pm 06-sep-12 01.50.10.000000 pm +08:00 06-sep-12 01.50.10.000000 pm
01-jan-09 12.00.00.000000 am 01-jan-09 12.00.00.000000 am +08:00 01-jan-09 05.01.01.000000 pm
sql> alter session set time_zone='+05:00';
session altered.
sql> select * from test_zone;
timestamp_dt zone_dt local_zone_dt (提前了三个小时)
----------------------------------- ----------------------------------- -----------------------------------
06-sep-12 01.50.10.000000 pm 06-sep-12 01.50.10.000000 pm +08:00 06-sep-12 10.50.10.000000 am
01-jan-09 12.00.00.000000 am 01-jan-09 12.00.00.000000 am +08:00 01-jan-09 02.01.01.000000 pm
sql> select * from v$timezone_names where rownum tzname tzabbrev
---------------------------------------------------------------- ----------------------------------------------------------------
africa/abidjan lmt
africa/abidjan gmt
africa/accra lmt
africa/accra gmt
africa/accra ghst
africa/addis_ababa lmt
africa/addis_ababa admt
africa/addis_ababa eat
africa/algiers lmt
sql> select tz_offset('africa/accra') from dual;
tz_offs
-------
+00:00
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';--设置会话日期显示格式
sql> select to_timestamp('20120909 00:01:02.123456789','yyyymmdd hh24:mi:ss.ff') from dual; --to_timestamp函数使用
to_timestamp('2012090900:01:02.123456789','yyyymmddhh24:mi:ss.ff')
---------------------------------------------------------------------------
09-sep-12 12.01.02.123456789 am
sql> select to_timestamp_tz('20120909 00:01:02.123456789 0:0','yyyymmdd hh24:mi:ss.ff tzh:tzm') from dual; --to_timestamp_tz使用
to_timestamp_tz('2012090900:01:02.1234567890:0','yyyymmddhh24:mi:ss.fftzh:t
---------------------------------------------------------------------------
09-sep-12 12.01.02.123456789 am +00:00
postgresql时间类型:9.1.2版本
名字 存储空间 描述 最低值 最高值 分辨率
timestamp[无时区] 8字节 包括日期和时间 4713 bc 5874897ad 1毫秒/14位
timestamp[含时区] with time zone 8字节 日期和时间,带时区 4713 bc 5874897ad 1毫秒/14位
interval 12字节 时间间隔 -178000000年 178000000年 1毫秒/14位
date 4字节 只用于日期 4713 bc 32767ad 1天
time[无时区] 8字节 只用于一日内时间 00:00:00 24:00:00 1毫秒/14位
postgres=# select current_timestamp; --可以看到精度6位,显示时区+8,和oracle的timestamp with time zone类型是一致的
now
-------------------------------
2012-09-06 14:04:51.363932+08
postgres=# create table test_t (time_col time,date_col date,timestamp_col timestamp);类型区别
create table
postgres=# insert into test_t values(now(),now(),now());
insert 0 1
postgres=# select * from test_t;
time_col | date_col | timestamp_col
-----------------+------------+----------------------------
14:19:24.277477 | 2012-09-06 | 2012-09-06 14:19:24.277477
(1 row)
精度和时区的控制:
postgres=# create table test_t1 (time_col time,date_col date,timestamp_col timestamp,timestamp_col0 timestamp(0) without time zone);
create table
postgres=# insert into test_t1 values(now(),now(),now(),now());
insert 0 1
postgres=# select * from test_t1;
time_col | date_col | timestamp_col | timestamp_col0
-----------------+------------+----------------------------+---------------------
14:34:59.840947 | 2012-09-06 | 2012-09-06 14:34:59.840947 | 2012-09-06 14:35:00
postgres=# alter table test_t1 add column timestamp_col1 timestamp(0) with time zone;
alter table
postgres=# insert into test_t1 values(now(),now(),now(),now(),now());
insert 0 1
postgres=# select * from test_t1;
time_col | date_col | timestamp_col | timestamp_col0 | timestamp_col1
-----------------+------------+----------------------------+---------------------+------------------------
14:34:59.840947 | 2012-09-06 | 2012-09-06 14:34:59.840947 | 2012-09-06 14:35:00 |
14:36:31.265579 | 2012-09-06 | 2012-09-06 14:36:31.265579 | 2012-09-06 14:36:31 | 2012-09-06 14:36:31+08
由此可见,timestamp本身不带时区,但是带精度,如果需要带时区,那么加上with time zone即可
postgres=# select now()::timestamp(0) without time zone;
now
---------------------
2012-09-06 14:42:12
[1] [2]
