南大通用GBase8a关于圆括号、逻辑操作符、转换操作符&函数和日期运算符的用法

发布日期:2024-09-09 09:49    点击次数:117

原文链接:https://www.gbase.cn/community/post/4357

更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。

今天给大家讲一下GBase 8a MPP Cluster中关于圆括号、逻辑操作符、转换操作符&函数,以及日期运算符的用法

1、圆括号

说明:(...) 括号,使用它来规定一个表达式的运算顺序,放在括号里的操作符优先执行。

示例

示例1:不使用括号,表达式先执行乘法操作,再执行加法操作。

gbase> SELECT 1+2*3 FROM dual;

+-------+

1+2*3 |

+-------+

7 |

+-------+

1 row in set

示例2:使用括号,表达式先执行括号中的加法操作,再执行括号外的乘法操作。

gbase> SELECT (1+2)*3 FROM dual;

+---------+

(1+2)*3 |

+---------+

9 |

+---------+

1 row in set

2 逻辑操作符

概述:在SQL中,所有的逻辑操作符返回的值均为TRUE、FALSE或NULL(UNKNOWN),它们是由1(TRUE)、0(FALSE)和NULL来表示的。

2.1 NOT,!逻辑非

操作符说明:如果操作数为0,返回1;如果操作数为非零,返回0;如果操作数为NULL,返回NULL。

示例

示例1:操作数为非零,返回值为0。

gbase> SELECT NOT 10 FROM dual;

+--------+

NOT 10 |

+--------+

0 |

+--------+

1 row in set

示例2:操作数为0,返回值为1。

gbase> SELECT NOT 0 FROM dual;

+-------+

NOT 0 |

+-------+

1 |

+-------+

1 row in set

示例3:操作数为NULL,返回值为NULL。

gbase> SELECT NOT NULL FROM dual;

+-----------+

NOT NULL |

+-----------+

NULL |

+-----------+

1 row in set

示例4:表达式的值为非零,返回值为0。

gbase> SELECT ! (1+1) FROM dual;

+---------+

! (1+1) |

+---------+

0 |

+---------+

1 row in set

示例5:表达式! 1+1与(!1)+1等价,执行结果为1。

gbase> SELECT ! 1+1 FROM dual;

+-------+

! 1+1 |

+-------+

1 |

+-------+

1 row in set

gbase> SELECT (!1)+1 FROM dual;

+--------+

(!1)+1 |

+--------+

1 |

+--------+

1 row in set

示例6:..NOT IN…

gbase> SELECT 1 NOT IN (2,3,null) FROM dual;

+---------------------+

1 NOT IN (2,3,null) |

+---------------------+

NULL |

+---------------------+

1 row in set

2.2 XOR逻辑异或

语法:a XOR b等价于(a AND (NOT b)) OR ((NOT a) AND b)

操作符说明:当任意一个操作数为NULL时,返回值为NULL。对于非NULL的操作数:

就是说两个值不相同,则异或结果为真,反之,为假。

示例

示例1:操作数不是NULL,真异或真,结果为假,即返回值为0。

gbase> SELECT 1 XOR 1 FROM dual;

+---------+

1 XOR 1 |

+---------+

0 |

+---------+

1 row in set

示例2:操作数不是NULL,真异或假,结果为真,即返回值为1。

gbase> SELECT 1 XOR 0 FROM dual;

+---------+

1 XOR 0 |

+---------+

1 |

+---------+

1 row in set

示例3:任意一个操作数为NULL,则结果为NULL。

gbase> SELECT 1 XOR NULL FROM dual;

+-------------+

1 XOR NULL |

+-------------+

NULL |

+-------------+

1 row in set

gbase> SELECT 0 XOR NULL FROM dual;

+-------------+

0 XOR NULL |

+-------------+

NULL |

+-------------+

1 row in set

示例4:a XOR b等价于(a AND (NOT b)) OR ((NOT a) AND b)。

gbase> SELECT 1 XOR 0 FROM dual;

+---------+

1 XOR 0 |

+---------+

1 |

+---------+

1 row in set

gbase> SELECT (1 AND (NOT 0)) OR ((NOT 1) AND 0) ;

+------------------------------------+

(1 AND (NOT 0)) OR ((NOT 1) AND 0) |

+------------------------------------+

1 |

+------------------------------------+

1 row in set

示例5:将同一个数异或比较后的结果,再次与该数进行异或比较,则结果为1。

gbase> SELECT 1 XOR 1 XOR 1 FROM dual;

+---------------+

1 XOR 1 XOR 1 |

+---------------+

1 |

+---------------+

1 row in set

3 转换操作符和函数3.1 BINARY

操作符说明:在字符串前使用BINARY操作符,可以区分大小写进行参数值的比较。

示例

示例1:字符串前不使用BINARY,比较不区分大小写。

gbase> SELECT 'a' = 'A' FROM dual;

+-----------+

'a' = 'A' |

+-----------+

1 |

+-----------+

1 row in set

gbase> SELECT 'a' = 'a ' FROM dual;

+------------+

'a' = 'a ' |

+------------+

1 |

+------------+

1 row in set

示例2:字符串前使用BINARY,比较区分大小写。

gbase> SELECT BINARY 'a' = 'A' FROM dual;

+------------------+

BINARY 'a' = 'A' |

+------------------+

0 |

+------------------+

1 row in set

示例3:字符串前使用BINARY,对尾空格进行比较。

gbase> SELECT BINARY 'a' = 'a ' FROM dual;

+-------------------+

BINARY 'a' = 'a ' |

+-------------------+

0 |

+-------------------+

1 row in set

3.2 CAST和CONVERT函数

语法:CAST(expr AS type),CONVERT(expr,type),CONVERT(expr USING transcoding_name)

函数说明:CAST()和CONVERT()函数用于将一个类型的数值转换到另一个类型。

type可以是下列值之一:

CHAR、DATE、DATETIME、DECIMAL、TIME、NUMERIC、INT、FLOAT、DOUBLE、VARCHAR、TIMESTAMP。 CAST()和CONVERT(...USING...)是标准的SQL语法。 CAST(str AS BINARY)等价于BINARY str。 CAST(expr AS CHAR)把表达式看作是默认字符集中的字符串。 CAST(expr AS float(M,D))、CAST(expr AS double(M,D))中M最大值255,D最大值30。 CAST(expr AS Float(X))指定长度,当X<24时,按照float处理;当24<X<=53时按double的最大长度和精度处理

注意:

使用CAST()函数改变列类型为DATE,DATETIME或TIME,只是标识此列,使其变为一个指定的数据类型,而不是改变列的值。 CAST()的最终执行结果将会转化为指定的列类型。 查询时将数据使用cast转化为varchar(0)会输出空串,使用create table as select from 从已有表中查询非空列进行转换varchar建新表,如果非空列转换成varchar(0)会报错。 cast as timestamp默认处理方式为支持将‘2020-01-02 11:11:12.123451’转化为timestamp,但create as select cast(…as timestamp)截断到秒级。 函数能将‘1970-01-01 00:00:01~2038-01-10 03:14:07’的UTC时间格式字符串转化为timestamp类型,但是timestamp存储最大值为,2038-01-01 00:59:59。 如需Timestamp支持精度到微秒,需要开启参数:_gbase_timestamp_append_prec=1

示例

示例1:将NOW()转换为DATE类型。

gbase> SELECT CAST(NOW() AS DATE) FROM dual;

+---------------------+

CAST(NOW() AS DATE) |

+---------------------+

2020-04-01 |

+---------------------+

1 row in set

示例2:字符串和数字类型的转换是隐式操作,用户使用时只要把字符串值当做一个数字即可。

gbase> SELECT 1+'1' FROM dual;

+-------+

1+'1' |

+-------+

2 |

+-------+

1 row in set

示例3:CAST(str AS BINARY)等价于BINARY str。

gbase> SELECT CAST('a' AS BINARY) = 'a ' FROM dual;

+----------------------------+

CAST('a' AS BINARY) = 'a ' |

+----------------------------+

0 |

+----------------------------+

1 row in set

gbase> SELECT 'A' = 'a ';

+------------+

'A' = 'a ' |

+------------+

1 |

+------------+

1 row in set (Elapsed: 00:00:00.00)

gbase> SELECT BINARY 'A' = 'a ' FROM dual;

+-------------------+

BINARY 'A' = 'a ' |

+-------------------+

0 |

+-------------------+

1 row in set

示例4:CAST(str AS varchar(X))示例

gbase> select cast('1.2345' as varchar) as data_varchar;

+--------------+

data_varchar |

+--------------+

1.2345 |

+--------------+

1 row in set (Elapsed: 00:00:00.00)

gbase> select cast('1.2345' as varchar(10)) as data_varchar;

+--------------+

data_varchar |

+--------------+

1.2345 |

+--------------+

1 row in set (Elapsed: 00:00:00.00)

gbase> select cast('1.2345' as varchar(3)) as data_varchar;

+--------------+

data_varchar |

+--------------+

1.2 |

+--------------+

1 row in set, 1 warning (Elapsed: 00:00:00.00)

gbase> select cast('1.2345' as varchar(0)) as data_varchar;

+--------------+

data_varchar |

+--------------+

+--------------+

1 row in set, 1 warning (Elapsed: 00:00:00.01)

gbase> create table t3 as select cast(a as varchar) as a from t;

Query OK, 2 rows affected (Elapsed: 00:00:00.51)

gbase> desc t3;

+-------+-------------+------+-----+---------+-------+

Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

a | varchar(11) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

1 row in set (Elapsed: 00:00:00.00)

gbase> create table t4 as select cast(a as varchar(0)) as a from t;

ERROR 1705 (HY000): gcluster DML error: [192.168.146.21:5050](GBA-02AD-0005)Failed to query in gnode:

DETAIL: Truncated incorrect CHAR(0) value: '1'

SQL: SELECT /*192.168.146.20_6_31_2021-01-14_15:25:42*/ /*+ TID('111') */ cast(`vcname000001.testdb.t`.`a` as char(0)) AS `a` FROM `testdb`.`t_n1` `vcname000001.testdb.t` target into server (HOST '192.168.146.21,192.168.146.20', PORT 5050, USER 'root', PASSWORD '', DATABASE 'testdb', TABLE 't4_n1', COMMENT 'col_seq 0, table_host 0 0 1, scn 18, distribution 1' )

3.3 TO_SINGLE_BYTE

语法:TO_SINGLE_BYTE(arg)

函数说明

将传入的arg从全角字符转半角字符。arg可以是任何类型的值和列,如果arg为字符串,并且字符串里面含有全角的话,在输出结果中就会将全角字符转为半角字符,其他字符保持不变。

该函数仅在UTF8字符集和GBK字符集下有效。当前仅95个字符支持全角转半角。95个字符如下:

create as select时候,包含函数列的字段类型根据查询结果的字段类型来确定,如果是查询结果的字段类型为字符类型,会根据结果的最大长度来判断是varchar、longblob类型。

注意:

只有VARCHAR、CHAR、TEXT支持字符串类型的列类型支持全角字符,并且使用to_single_byte转换成功。 LONGBLOB、BLOB虽然能存放全角字符,但是是按二进制存储的,TO_SINGLE_BYTE转换后还是全角字符。 BLOB类型经TO_SINGLE_BYTE转换后为VARBINARY类型

示例

create table t(a int, b varchar(10), c datetime, t text, e longblob, f blob, g char(10));

gbase> insert into t values(1, 'aaaaaa', '2011-01-01 11:11:11', 'aaaa', 'aaaa', 'aaaa', 'aaaa');

Query OK, 1 row affected (Elapsed: 00:00:00.05)

gbase> select to_single_byte(a) as sing_a,to_single_byte(b) as sing_b, to_single_byte(c) as sing_c, to_single_byte(t) as sing_t, to_single_byte(e) as sing_e, to_single_byte(f) as sing_f, to_single_byte(g) as sing_g from t;

+--------+--------+---------------------+--------+--------------+--------------+------------+

sing_a | sing_b | sing_c | sing_t | sing_e | sing_f | sing_g |

+--------+--------+---------------------+--------+--------------+--------------+------------+

1 | aaaaaa | 2011-01-01 11:11:11 | aaaa | aaaa | aaaa | aaaa |

+--------+--------+---------------------+--------+--------------+--------------+------------+

1 row in set (Elapsed: 00:00:00.03)

gbase> create table ty as select to_single_byte(a) as sing_a,to_single_byte(b) as sing_b, to_single_byte(c) as sing_c, to_single_byte(t) as sing_t, to_single_byte(e) as sing_e, to_single_byte(f) as sing_f, to_single_byte(g) as sing_g from t;

Query OK, 1 row affected (Elapsed: 00:00:00.11)

gbase> show create table ty \G

*************************** 1. row ***************************

Table: ty

Create Table: CREATE TABLE "ty" (

"sing_a" varchar(11) DEFAULT NULL,

"sing_b" varchar(10) DEFAULT NULL,

"sing_c" varchar(26) DEFAULT NULL,

"sing_t" varchar(10922) DEFAULT NULL,

"sing_e" longblob,

"sing_f" varbinary(32767) DEFAULT NULL,

"sing_g" varchar(10) DEFAULT NULL

) ENGINE=EXPRESS DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace'

1 row in set (Elapsed: 00:00:00.00)

gbase> select * from ty;

+--------+--------+---------------------+--------+--------------+--------------+------------+

sing_a | sing_b | sing_c | sing_t | sing_e | sing_f | sing_g |

+--------+--------+---------------------+--------+--------------+--------------+------------+

1 | aaaaaa | 2011-01-01 11:11:11 | aaaa | aaaa | aaaa | aaaa |

+--------+--------+---------------------+--------+--------------+--------------+------------+

1 row in set (Elapsed: 00:00:00.02)

4 日期算术运算

语法说明:日期 +(-) bit_expr

同以下语法等价:日期 +(-) interval expr type

运算说明:日期加减运算跟普通的加减运算逻辑一样,只是后面加的数字、字符或表达式的单位为天数。该语法是在date类型、datetime类型、timestamp类型变量后面加(或减去)指定的bit_expr的天数。

示例

示例1:CAST('2019-06-18' as date) + 30为日期,返回增加30天后的日期。

gbase> SELECT CAST('2019-06-18' as date) + 30 FROM dual;

+----------------------------------+

CAST('2019-06-18' as date) + 30 |

+----------------------------------+

2019-07-18 |

+----------------------------------+

1 row in set

以上就是今天的内容,感谢大家阅读!

原文链接:https://www.gbase.cn/community/post/4357

更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。

gbaseassetbyte示例发布于:天津市声明:该文观点仅代表作者本人,搜狐号系信息发布平台,搜狐仅提供信息存储空间服务。