lightdb 的 nvl 函数支持 nvl(null::number,‘0’)
背景
在业务产品中,存在 oracle 移植过来的 sql 语句。它们使用 nvl(null::number,‘0’)。lightdb 23.4 版本 oracle 模式对此进行了支持。
lightdb 关于 nvl(null::number,‘0’)
lightdb 23.4 版本之前 nvl(null::number, # ) 参数 # 仅支持数字类型,使用其它类型时将会报错。
lightdb 23.4 版本之后 nvl(null::number, # ) 扩充了使用场景。在 oracle 模式函数 nvl 中参数 # 支持使用任意类型。如果可以正常转换成数字类型将不会报错。
nvl(null::数值类型, unknown) 示例
SELECT nvl(null::numeric, '-025');
nvl
-----
-25
(1 row)
SELECT nvl(null::integer, ' +023');
nvl
-----
23
(1 row)
SELECT nvl(null:: double precision, ' .011');
nvl
-------
0.011
(1 row)
nvl(null::数值类型, varchar) 示例
-- CREATE TABLE
CREATE TABLE stadium (
id INT PRIMARY KEY,
name VARCHAR(255),
capacity INTEGER,
location VARCHAR(255),
mark VARCHAR(255),
construct_year INTEGER
);
-- INSERT INTO
INSERT INTO stadium (id, name, capacity, location, mark, construct_year)
VALUES (1, 'huaicheng', 50000, '39.123, 33.234', '0.', 2000);
INSERT INTO stadium (id, name, capacity, location, mark, construct_year)
VALUES (2, 'huaicheng', 60000, '39.123, 33.234', '.0', 2005);
INSERT INTO stadium (id, name, capacity, location, mark, construct_year)
VALUES (3, 'huaicheng', 70000, '39.123, 33.234', '+1', 2010);
INSERT INTO stadium (id, name, capacity, location, mark, construct_year)
VALUES (4, 'huaicheng', 80000, '39.123, 33.234', '-1', 2015);
INSERT INTO stadium (id, name, capacity, location, mark, construct_year)
VALUES (5, 'huaicheng', 90000, '39.123, 33.234', '0', 2020);
INSERT INTO stadium (id, name, capacity, location, mark, construct_year)
VALUES (6, 'huaicheng', 40000, '39.123, 33.234', '1', 2020);
-- TEST SELECT VARCHAR
SELECT nvl(null::numeric, mark) FROM stadium;
nvl
-----
0
0.0
1
-1
0
1
(6 rows)