Oracle日期时间类型有两类,一类是日期时间类型,包括Date, Timestamp with time zone, Timestamp with local time zone。另一类是Interval类型,主要有Interval year to month 和Interval day to second两种。

KingbaseES也有类似的两类。其中的日期时间类型包括Timestamp with time zone, Timestamp without time zone,Date,Time with time zone , Time without time zone五种。Interval类型就是Interval。

KingbaseES扩展了Date数据类型,在兼容oracle模式时date的值是“年月日时分秒”与oracle一致,在pg模式下则为“年月日”。

一、日期时间类型

Date 类型:

  • Oracle的Date类型包括年、月、日、时、分、秒六个字段, 时间跨度是公元前4712年1月1日~公元9999年12月31日。
  • KingbaseES在兼容oracle模式下可以直接使用date数据类型或者使用 Timestamp(0) without time zone 类型来对应。时间跨度从公元前4713年~公元294276年。

1、Oracle date

  1. SQL> create table o_test(value date);
  2. Table created.
  3. SQL> insert into o_test values(to_date('-4712-01-01 00:30:45', 'syyyy-mm-dd hh24:mi:ss'));
  4. 1 row created.
  5.  
  6. SQL> insert into o_test values(to_date('-4712-01-01 00:30:45', 'syyyy-mm-dd hh24:mi:ss') - interval '1' day);
  7. insert into o_test values(to_date('-4712-01-01 00:30:45', 'syyyy-mm-dd hh24:mi:ss') - interval '1' day) *
  8. ERROR at line 1:
  9. ORA-01841: (full) year must be between -4713 and +9999, and not be 0
  10.  
  11. SQL> insert into o_test values(to_date('9999-12-31 12:30:45', 'yyyy-mm-dd hh24:mi:ss'));
  12. 1 row created.
  13. SQL> insert into o_test values(to_date('9999-12-31 12:30:45', 'yyyy-mm-dd hh24:mi:ss') + interval '1' day);
  14. insert into o_test values(to_date('9999-12-31 12:30:45', 'yyyy-mm-dd hh24:mi:ss') + interval '1' day)
  15. *
  16. ERROR at line 1:
  17. ORA-01841: (full) year must be between -4713 and +9999, and not be 0
  18.  
  19. SQL> select to_char(value, 'syyyy/mm/dd hh24:mi:ss') from o_test;
  20.  
  21. TO_CHAR(VALUE,'SYYYY/MM/DDHH24:MI:SS')
  22. ----------------------------------------
  23. -4712/01/01 00:30:45
  24. 9999/12/31 12:30:45

2、KingbaseES Date

KingbaseES date 类型实际 就是 Timestamp(0) without time zone

  1. test=# create table test1(value timestamp(0) without time zone, value1 date);
  2. CREATE TABLE
  3. test=# insert into test1 values('4712-01-01 00:30:45BC','4712-01-01 00:30:45BC');
  4. INSERT 0 1
  5. test=# insert into test1 values(to_timestamp('4712-01-01 00:30:45BC','yyyy-mm-dd hh24:mi:ssBC') - interval '1 day',to_timestamp('4712-01-01 00:30:45BC','yyyy-mm-dd hh24:mi:ssBC') - interval '1 day');
  6. INSERT 0 1
  7. test=# insert into test1 values('9999-12-31 23:59:59','9999-12-31 23:59:59');
  8. INSERT 0 1
  9. test=# insert into test1 values(to_timestamp('9999-12-31 23:59:59','yyyy-mm-dd hh24:mi:ss') + interval '1 day',to_timestamp('9999-12-31 23:59:59','yyyy-mm-dd hh24:mi:ss') + interval '1 day');
  10. INSERT 0 1
  11.  
  12. test=# select * from test1;
  13. value | value1
  14. ------------------------+------------------------
  15. 4712-01-01 00:30:45 BC | 4712-01-01 00:30:45 BC
  16. 4713-12-31 00:30:45 BC | 4713-12-31 00:30:45 BC
  17. 9999-12-31 23:59:59 | 9999-12-31 23:59:59
  18. 10000-01-01 23:59:59 | 10000-01-01 23:59:59
  19. (4 行记录)

二、Timestamp(p)类型

在Timestamp(p) 数据类型上两种数据库基本一致,除了毫秒精度上的区别。

  • Oracle 毫秒的精度最大为9位,默认为6位。数据中的毫秒数如果小于精度,会在有效数据后自动以0补足位数。
  • KingbaseES 毫秒精度最大为6位,默认为6位,当指定精度超过6位时表依然会创建但是会有警告提示并将精度自动修正为6位。在兼容oracle模式时会在末尾补0,pg模式不会补足。

1、Oracle Timestamp

  1. SQL> create table o_test(value timestamp);
  2.  
  3. Table created.
  4. SQL> insert into o_test values(to_timestamp('2021-10-1 12:30:50.123', 'yyyy-mm-dd hh24:mi:ss.ff'));
  5. 1 row created.
  6. SQL> insert into o_test values(to_timestamp('2021-10-1 12:30:50.123456', 'yyyy-mm-dd hh24:mi:ss.ff6'));
  7. 1 row created.
  8. SQL> insert into o_test values(to_timestamp('2021-10-1 12:30:50.123456789', 'yyyy-mm-dd hh24:mi:ss.ff9'));
  9. 1 row created.
  10.  
  11. SQL> select * from o_test;
  12. VALUE
  13. ---------------------------------------------------------------------------
  14. 01-OCT-21 12.30.50.123000 PM
  15. 01-OCT-21 12.30.50.123456 PM
  16. 01-OCT-21 12.30.50.123457 PM

Oracle Timestamp 指定精度

  1. SQL> create table o_test1(value timestamp(9));
  2. Table created.
  3. SQL> insert into o_test1 values(to_timestamp('2021-10-1 11:30:50.123456789', 'yyyy-mm-dd hh24:mi:ss.ff9'));
  4. 1 row created.
  5. SQL> insert into o_test1 values(to_timestamp('2021-10-1 11:30:50.123', 'yyyy-mm-dd hh24:mi:ss.ff'));
  6. 1 row created.
  7. SQL> select * from o_test1;
  8. VALUE
  9. ---------------------------------------------------------------------------
  10. 01-OCT-21 11.30.50.123456789 AM
  11. 01-OCT-21 11.30.50.123000000 AM

2、KingbaseES Timestamp without time zone

  1. test=# create table test1(value timestamp without time zone);
  2. CREATE TABLE
  3. test=# insert into test1 values('2021-10-1 12:30:45.123456');
  4. INSERT 0 1
  5. test=# insert into test1 values('2021-10-1 12:30:45.123456789');
  6. INSERT 0 1
  7. test=# insert into test1 values('2021-10-1 12:30:45.123');
  8. INSERT 0 1
  9. 兼容oracle模式
  10. test=# select * from test1;
  11. value
  12. ----------------------------
  13. 2021-10-01 12:30:45.123456
  14. 2021-10-01 12:30:45.123457
  15. 2021-10-01 12:30:45.123000
  16.  
  17. 兼容pg模式
  18. test=# select * from test1;
  19. value
  20. ----------------------------
  21. 2021-10-01 12:30:45.123456
  22. 2021-10-01 12:30:45.123457
  23. 2021-10-01 12:30:45.123

KingbaseES Timestamp without time zone指定精度

  1. test=# create table test2(value timestamp(9) without time zone);
  2. 警告: TIMESTAMP(9)减少到最大允许值,6
  3. 1create table test2(value timestamp(9) without time zone);
  4. ^
  5. 警告: TIMESTAMP(9)减少到最大允许值,6
  6. 警告: TIMESTAMP(9)减少到最大允许值,6
  7. CREATE TABLE
  8.  
  9. test=# create table test2(value timestamp(3) without time zone);
  10. CREATE TABLE
  11. test=# insert into test2 values('2021-10-1 12:30:45.123456789');
  12. INSERT 0 1
  13. test=# insert into test2 values('2021-10-1 12:30:45.123');
  14. INSERT 0 1
  15. test=# insert into test2 values('2021-10-1 12:30:45.1');
  16. INSERT 0 1
  17. test=# select * from test2;
  18. value
  19. -------------------------
  20. 2021-10-01 12:30:45.123
  21. 2021-10-01 12:30:45.123
  22. 2021-10-01 12:30:45.100

三、Timestamp(p) with time zone

带时区的时间类型,两种数据库之间的区别: Oracle会保存输入的的时区,而KingbaseES是把数据自动转换成为数据库时区对应的时间 (时区值由kingbase.conf中的timezone参数设定,可以使用set timezone来进行更改)。在使用时需要注意这个变化。

1、Oracle

  1. SQL> create table o_test( value timestamp with time zone);
  2. Table created.
  3. SQL> insert into o_test values(systimestamp);
  4. 1 row created.
  5. SQL> insert into o_test values(to_timestamp_tz('2012-12-31 12:30:50.123456 +10:00', 'yyyy-mm-dd hh24:mi:ss.ff tzh:tzm'));
  6. 1 row created.
  7. SQL> select * from o_test;
  8. VALUE
  9. ---------------------------------------------------------------------------
  10. 14-OCT-21 11.33.12.639156 AM +08:00
  11. 31-DEC-12 12.30.50.123456 PM +10:00

2、KingbaseES

  1. test=# create table test3(value timestamp with time zone);
  2. CREATE TABLE
  3. test=# insert into test3 values(current_timestamp);
  4. INSERT 0 1
  5. test=# insert into test3 values('2021-12-31 12:30:50.123456+10');
  6. INSERT 0 1
  7. test=# insert into test3 values('2021-12-31 12:30:50.123456 PST');
  8. INSERT 0 1
  9. test=#
  10. test=# select * from test3;
  11. value
  12. -------------------------------
  13. 2021-10-14 11:31:10.820010+08
  14. 2021-12-31 10:30:50.123456+08
  15. 2022-01-01 04:30:50.123456+08

四、interval

Oracle的Interval类型主要分为Interval year to month 和 Interval day to second两类。分别对应间隔为年月和间隔为日、时、分、秒的情况。

KingbaseES的interval提供了比较丰富的扩展:interval[fields][(p)]  ,interval类型有一个附加选项,它可以通过写下面之一的短语来限制存储的fields的集合:YEAR,MONTH,DAY,HOUR,MINUTE,SECOND,YEAR TO MONTH,DAY TO HOUR,DAY TO MINUTE, DAY TO SECOND,HOUR TO MINUTE,HOUR TO SECOND,MINUTE TO SECOND

注意如果fields和p被指定,fields必须包括SECOND,因为精度只应用于秒。

1、Oracle

Interval year(p) to month类型

Oracle的Interval year(p) to month类型表示年月的时间间隔。year, month一起使用的时候,写法类似于Interval '12-11' year to month。其中的年份根据精度p来决定,p值为1~9,默认为2。月份只能是0~11之间的值。单独使用month时精度也是控制年份的。

  1. SQL> create table test(value interval year(3) to month);
  2. Table created.
  3. SQL> insert into test values(interval '100-5' year to month);
  4. insert into test values(interval '100-5' year to month) *
  5. ERROR at line 1:
  6. ORA-01873: the leading precision of the interval is too small
  7. SQL> insert into test values(interval '100-5' year(3) to month);
  8. 1 row created.
  9. SQL> insert into test values(interval '1299' month);
  10. insert into test values(interval '1299' month)
  11. *
  12. ERROR at line 1:
  13. ORA-01873: the leading precision of the interval is too small
  14. SQL> insert into test values(interval '1299' month(3));
  15. 1 row created.
  16.  
  17. SQL> select * from test;
  18. VALUE
  19. ---------------------------------------------------------------------------
  20. +100-05
  21. +108-03

2、KingbaseES

  1. KingbaseES 可以直接使用interval类型的数据,使用上比较灵活。
  2. test=# create table test4(value interval);
  3. CREATE TABLE
  4. test=# insert into test4 values('100-5');
  5. INSERT 0 1
  6. test=# insert into test4 values('90-12');
  7. 错误: 间隔字段超出范围: "90-12"
  8. 1insert into test4 values('90-12');
  9. ^
  10. test=# insert into test4 values('90-11');
  11. INSERT 0 1
  12. test=# insert into test4 values('90-0');
  13. INSERT 0 1
  14. test=# insert into test4 values('10000 year');
  15. INSERT 0 1
  16. test=# insert into test4 values('12000 month');
  17. INSERT 0 1
  18. test=# insert into test4 values('1000 year 12000 month');
  19. INSERT 0 1
  20. test=# insert into test4 values('80000 hour');
  21. INSERT 0 1
  22. test=# select * from test4;
  23. value
  24. --------------------
  25. 100 years 5 mons
  26. 90 years 11 mons
  27. 90 years
  28. 10000 years
  29. 1000 years
  30. 2000 years
  31. 80000:00:00.000000

  

KingbaseES 时间类型数据和oracle时间类型的区别的更多相关文章

  1. Hibernate二进制或大文件类型数据和Oracle交互

    //测试存储二进制文件 @Test public void test() throws IOException{  InputStream in=new FileInputStream("E ...

  2. mybatis 处理CLOB/BLOB类型数据

    BLOB和CLOB都是大字段类型. BLOB是按二进制来存储的,而CLOB是可以直接存储文字的. 通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.文章或者是较长的文字 ...

  3. hbase中double类型数据做累加

    public static Result incr(String tableFullName, String rowKey, String family, String qualifier, long ...

  4. HighCharts 图表插件 自定义绑定 时间轴数据

    HighCharts 图表插件 自定义绑定 时间轴数据,解决时间轴自动显示数据与实际绑定数据时间不对应问题! 可能要用到的源码片段:http://code.662p.com/list/14_1.htm ...

  5. 2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语

    数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6 ...

  6. mybatis查询日期时间数据得到long类型数据的问题

    使用mybatis查询数据时,如果数据库存储的是timestamp.datetime.date.time等时间类型,而Java bean也使用的是date类型,mybatis会自动将date类型转换为 ...

  7. mysql那些事(2)时间类型数据如何存储

    几乎每次数据库建模的时候,都会遇到时间类型数据存储的问题. mysql存储时间通常选择这四种类型:datetime.timestamp.int和bigint四种方式,到底使用什么类型,需要看具体的业务 ...

  8. Oracle 时间,日期 类型函数及参数详解

    ORACLE字符数字日期之间转化   Java代码   24 小时的形式显示出来要用 HH24       select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss' ...

  9. 45.oracle表类型、数据拆分、表分区

    不要做一些没有意义的事情,就比如说你要离职并不打算吃回头草,离职理由中完全没有必要说明“领导的水平太渣,人品太差”此类的原因,而是“个人原因”,当然实在不批准辞职另说. oracle表类型 表的类型分 ...

随机推荐

  1. Python基础知识+题目练习,我不信你能做出这道题

    函数式编程 高阶函数 Python学习交流Q群:660193417#### map(func, *iterable) def fn(x, y, z): pass map(fn, range(10), ...

  2. 零基础学Java(1)初识Java程序

    前言 就国内来说,Java毫无疑问是后端语言中的No.1没有之一,所以今天我们也来0基础学习Java!!! Java的好处(针对测试工程师) 面试加分->涨薪 大多数公司服务端用的都是Java, ...

  3. Tapdata Cloud 2.1.2 来啦:大波细节已就绪!字段类型可批量修改、支持微信扫码登录、新增支持 Vika 为目标

    Tapdata Cloud cloud.tapdata.net 让数据实时可用 Tapdata Cloud 是国内首家异构数据库实时同步云平台,目前支持 Oracle.MySQL.PG.SQL Ser ...

  4. CRM汇客 牛刀小试 5个BUG修复

    1.权限管理-用户管理-高级搜索-手机号搜索不可用 1.1现象 1.2解决思路 1.2.1 定位接口 接口名:system/user/list 请求方式:GET请求 1.2.3 确定bug所在位置 b ...

  5. while循环--和do-while循环

    对于循环语句来说他会有一个回上去的箭头,这个回上去的箭头就形成了一个重复做的事情,那种重复做的事情我们就叫做循环 while循环 ~如果我们把while翻译作"当",那么一个whi ...

  6. Object类和Dome的新媒体类型

    Object类 所有的类都是继承自Object的 Java Object 类是所有类的父类,也就是说 Java 的所有类都继承了 Object,子类可以使用 Object 的所有方法 Object 类 ...

  7. Mac安装 Scrapy 报错 No local packages or working download links found for incremental>=16.10.1

    证书原因: wget http://curl.haxx.se/ca/cacert.pem mv cacert.pem ca-bundle.crt sudo mkdir -p /etc/pki/tls/ ...

  8. 适用于MES、WMS、ERP等管理系统的实体下拉框设计

    场景 该设计多适用于MES,ERP,WMS 等管理类型的项目. 在做管理类型项目的时候,前端经常会使用到下拉框,比如:设备,工厂等等.下拉组件中一般只需要他们的ID,Name属性,而这些数据都创建于其 ...

  9. 最佳实践 | 疫情之下,了解 eolink 如何助力远程办公!

    新冠疫情肆虐,eolink 结合自身长期的远程研发协作经验,为企业推出 API 管理远程协作指南,以下方案不仅在 Eolinker 内部,也在众多客户中得到验证,希望能够帮助您快速了解如何将 API ...

  10. 07 MySQL_SQL语言分类

    SQL语言分类 DDL Data Definition Language 数据定义语言 包括: create , alter ,drop , truncate; 不支持事务 DML Data Mani ...