SIGN 函数
SIGN 函数返回数字的符号(正或负)。SIGN 函数的结果为 1、-1 或 0,表示参数的符号。
语法
SIGN (number)
参数
- number
-
数字或计算结果为数字的表达式。它可以是 DECIMAL、FLOAT8 或 SUPER 类型。Amazon Redshift 可根据隐式转换规则转换其他数据类型。
返回类型
SIGN 返回与输入参数相同的数字数据类型。如果输入为 DECIMAL,则输出为 DECIMAL(1,0)。
当输入为 SUPER 类型时,输出将保留与输入相同的动态类型,而静态类型仍为 SUPER 类型。当 SUPER 的动态类型不是数字时,Amazon Redshift 将返回一个 null 值。
示例
确定为给定交易支付的佣金的符号:
select commission, sign (commission) from sales where salesid=10000; commission | sign -----------+------ 28.05 | 1 (1 row)
以下示例显示“t2.d”使用 double precision 作为其类型(因为输入是双精度),“t2.n”使用 numeric(1,0) 作为输出(因为输入是数字)。
CREATE TABLE t1(d double precision, n numeric(12, 2)); INSERT INTO t1 VALUES (4.25, 4.25), (-4.25, -4.25); CREATE TABLE t2 AS SELECT SIGN(d) AS d, SIGN(n) AS n FROM t1; \d t1 \d t2 The output of the “\d” commands is: Table "public.t1" Column | Type | Encoding | DistKey | SortKey | Preload | Encryption | Modifiers --------+------------------+----------+---------+---------+---------+------------+----------- d | double precision | none | f | 0 | f | none | n | numeric(12,2) | lzo | f | 0 | f | none | Table "public.t2" Column | Type | Encoding | DistKey | SortKey | Preload | Encryption | Modifiers --------+------------------+----------+---------+---------+---------+------------+----------- d | double precision | none | f | 0 | f | none | n | numeric(1,0) | lzo | f | 0 | f | none |