VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

DATETIME2类型

功能描述

Vastbase G100在SQLServer兼容模式下支持DATETIME2类型。DATETIME2类型结合了24小时制时间的日期,精确到小数点后7位,其格式为yyyy-MM-dd HH:mm:ss.fffffff。支持DATETIME2类型于char、varchar、nchar、datetime、date和time类型的隐式转换。

语法格式

datetime2[($n)]

参数说明

$n

用户指定的精度值,该参数可选。当未设置时,默认最大精度为7。

取值范围:0~7,其中0和7可取

  • 当精度大于7时,会提醒用户最大值为7,并强制将精度修改为7。

  • 当精度小于0时,会报错。

注意事项

  • 该功能仅在数据库兼容模式为SQL Server时支持(即数据库实例初始化时指定DBCOMPATIBILITY='MSSQL')。

  • 当插入字段值的精度超过预设的精度时,只保留值到预设精度,当插入值小于预设精度时,以插入值为准。

示例

示例1: 创建带datetime2数据类型的测试表,并插入数据。

1、创建带datetime2字段的测试表。

create table t_datetime2(id int,col1 datetime2,col2 datetime2(7));

2、向测试表中插入带公元、时区的记录。

insert into t_datetime2 values(0,'1999-01-08 12:00:00','1999-01-08 12:00:00');
insert into t_datetime2 values(1,'1999-01-08 12:00:00.1','1999-01-08 12:00:00.1');
insert into t_datetime2 values(2,'1999-01-08 12:00:00.12','1999-01-08 12:00:00.12');

3、查询插入记录在表中的情况。

select * from t_datetime2;

结果展示为:

 id |          col1          |          col2
----+------------------------+------------------------
  0 | 1999-01-08 12:00:00    | 1999-01-08 12:00:00
  1 | 1999-01-08 12:00:00.1  | 1999-01-08 12:00:00.1
  2 | 1999-01-08 12:00:00.12 | 1999-01-08 12:00:00.12
(3 rows)

示例2: 对datetime2字段指定不同的精度创建表。

1、创建带不同精度的datetime2字段的测试表。

create table test1(col1 datetime2(0));
create table test2(col1 datetime2(7));
create table test(col1 datetime2(8));
create table test(col1 datetime2(-1));

返回结果如下:

CREATE TABLE

CREATE TABLE

WARNING:  DATETIME2(8) precision reduced to maximum allowed, 7
LINE 1: create table test(col1 datetime2(8));
                               ^
WARNING:  DATETIME2(8) precision reduced to maximum allowed, 7
CREATE TABLE

ERROR:  syntax error at or near "-"
LINE 1: create table test(col1 datetime2(-1));
                                         ^
  • 前两条测试语句成功创建表。

  • 第三条语句创建表成功,但由于精度超过7,因此提示警告信息将精度修改为最高值7。

  • 第四条执行失败,因为精度值不能为负数。

示例3:datetime2与其他类型进行隐式转换。

1、创建测试表。

create table t_datetime2(id int,col1 datetime2,col2 datetime2(7));
create table t_othertype(id int,col1 char(30),col2 varchar(30),col3 nchar(30),col4 datetime,col5 smalldatetime,col6 date,col7 time);

2、向t_datetime2表中插入数据将其他类型转换为datetime2。

insert into t_datetime2 values(1,'1999-01-08 12:00:00.12345678'::char(30),'1999-01-08 12:00:00.12345678'::char(30));
insert into t_datetime2 values(2,'1999-01-08 12:00:00.12345678'::varchar(30),'1999-01-08 12:00:00.12345678'::varchar(30));
insert into t_datetime2 values(3,'1999-01-08 12:00:00.12345678'::nchar(30),'1999-01-08 12:00:00.12345678'::nchar(30));
insert into t_datetime2 values(4,'1999-01-08 12:00:00.123'::datetime,'1999-01-08 12:00:00.123'::datetime);
insert into t_datetime2 values(5,'1999-01-08 12:00:00'::smalldatetime,'1999-01-08 12:00:00'::smalldatetime);
insert into t_datetime2 values(6,'1999-01-08'::date,'1999-01-08'::date);
insert into t_datetime2 values(7,'12:00:00.123456'::time,'12:00:00.123456'::time);

3、查询表t_datetime2。

select * from t_datetime2;

返回结果为:

 id |            col1             |            col2
----+-----------------------------+-----------------------------
  1 | 1999-01-08 12:00:00.1234568 | 1999-01-08 12:00:00.1234568
  2 | 1999-01-08 12:00:00.1234568 | 1999-01-08 12:00:00.1234568
  3 | 1999-01-08 12:00:00.1234568 | 1999-01-08 12:00:00.1234568
  4 | 1999-01-08 12:00:00.123     | 1999-01-08 12:00:00.123
  5 | 1999-01-08 12:00:00         | 1999-01-08 12:00:00
  6 | 1999-01-08 00:00:00         | 1999-01-08 00:00:00
  7 | 1900-01-01 12:00:00.123456  | 1900-01-01 12:00:00.123456
(7 rows)

4、向t_othertype表中插入数据将datetime2转其他类型。

insert into t_othertype values(1,'1999-01-08 12:00:00.1234567'::datetime2,'1999-01-08 12:00:00.1234567'::datetime2,'1999-01-08 12:00:00.1234567'::datetime2,'1999-01-08 12:00:00.1235011'::datetime2,'1999-01-08 12:00:00.1234567'::datetime2,'1999-01-08 12:00:00.1234567'::datetime2,'1999-01-08 12:00:00.1234567'::datetime2);

5、查询表t_othertype。

select * from t_othertype;

返回结果为:

 id |              col1              |            col2             |              col3              |          col4           |        col5         |    col6    |      col7
----+--------------------------------+-----------------------------+--------------------------------+-------------------------+---------------------+------------+-----------------
  1 | 1999-01-08 12:00:00.1234567    | 1999-01-08 12:00:00.1234567 | 1999-01-08 12:00:00.1234567    | 1999-01-08 12:00:00.124 | 1999-01-08 12:00:00 | 1999-01-08 | 12:00:00.123456
(1 row)