文章结束给大家来个程序员笑话:[M]
oracle
date 括包时光和日期类型,括包年月日,小时,分钟,秒。这个经经常使用,相信大家都悉熟
timestamp 这个类型很确精,是确精到妙微的时光单位。指定精度的小数位,最多为9位,认默6位
timestamp with time zone 这个除了括包了timestamp的外,又有了时区。
timstamp with local time zone 这个类型不括包时区偏移量,由户用地当会话时区肯定。
interval year to month
interval day to second
timestamp可以通过to_timestamp_t转换到timestamp with time zone
tz_offset可以看查时区的差移位
看查数据库时区:
SELECT dbtimezone FROM dual;
select sessiontimezone from dual;
SQL> select extract(year from sysdate) from dual;--获得年份
EXTRACT(YEARFROMSYSDATE)
------------------------
2012
SQL> select extract(month from sysdate) from dual;--获得月份
EXTRACT(MONTHFROMSYSDATE)
-------------------------
9
SQL> select extract(day from sysdate) from dual;--获得日
EXTRACT(DAYFROMSYSDATE)
-----------------------
6
SQL> select from_tz(timestamp '2012-09-06 09:00:00','8:00') from dual; --timestamp转timestamp with time zone
FROM_TZ(TIMESTAMP'2012-09-0609:00:00','8:00')
---------------------------------------------------------------------------
06-SEP-12 09.00.00.000000000 AM +08:00
SQL> create table test_zone
2 (timestamp_dt TIMESTAMP,
3 zone_dt TIMESTAMP WITH TIME ZONE,
4 local_zone_dt TIMESTAMP WITH LOCAL TIME ZONE);
Table created.
SQL> insert into test_zone values (sysdate,sysdate,sysdate);
1 row created.
SQL> col TIMESTAMP_DT for a35
SQL> col ZONE_DT for a35
SQL> col LOCAL_ZONE_DT for a35
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
SQL> insert into test_zone values(to_date('20090101','yyyymmdd'),to_timestamp('20090101','yyyymmdd'),to_timestamp_tz('20090101 010101 -8:00','yyyymmdd hh24miss TZH:TZM'));
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<10; --看查时区名称
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
文章结束给大家分享下程序员的一些笑话语录:
神灯新篇
一个程序员在海滩上发现了一盏神灯。他在灯上擦了几下,一个妖怪就从灯里跳出来说:“我是世界上法术最强的妖怪。我可以实现你的任何梦想,但现在,我只能满足你一个愿望。”程序员摊开了一幅中东地图说:“我想让中东得到永久的和平。”妖怪答道:“哦,我没办法。自打创世纪以来,那里的战火就没有停息过。这世上几乎没有我办不到的事,但这件事除外。”程序员于是说:“好吧,我是一个程序员,为许多用户编写过程序。你能让他们把需求表述得更清楚些,并且让我们的软件项目有那么一两次按进度按成本完成吗?”妖怪说:“唔,我们还是来看中东地图吧。”