lightdb对oracle的nvl/nvl2函数的支持

一,oracle下的函数说明(详见链接)

        a) nvl函数

        b) nvl2函数

        c) 隐式转换表

二,nvl/nvl2函数返回类型

       通过上述官方文档,我们发现nvl/nvl2函数的返回值分别是以参数1/参数2的数据类型为参考的。当返回值能被转换成参数1/参数2所对应的数据类型时,则转换后再返回;如果无法转换,则报错。

     另外一种特殊的情况是数字类型,不管是nvl/nvl2还是decode/decode函数,为了防止精度丢失,oracle总是返回的类型,总是以参数中精度最大那个为准。以下是oracle官方文档的原文描述。

  • If expr2 is numeric data, then Oracle Database determines which argument has the highest numeric precedence, implicitly converts the other argument to that data type, and returns that data type.

三, lightdb对nvl/nvl2的支持

  a) lightdb中对nvl的函数定义(nvl2同理)

                               List of functions
 Name |      Result data type       |        Argument data types      
--------+------+-----------------------------+------------------------------------+------
 nvl  | bit                                           | bit, "any"                         
 nvl  | bytea                                      | bytea, "any"                       
 nvl  | date                                        | date, "any"                        
 nvl  | numeric                                  | double precision, "any"            
 nvl  | numeric                                  | integer, "any"                     
 nvl  | numeric                                  | numeric, "any"                     
 nvl  | text                                         | text, "any"                        
 nvl  | timestamp without time zone | timestamp without time zone, "any" 
 nvl  | timestamp with time zone      | timestamp with time zone, "any"    
 nvl  | time without time zone           | time without time zone, "any"      

 b) 使用示例

    1,     select pg_typeof(nvl('A', 1)), nvl('A', 1);
            pg_typeof | nvl 
            -----------+-----
             text      | A
            (1 row)

    2,     select pg_typeof(nvl(1.1, null)), nvl(1.1, null);
            pg_typeof | nvl 
            -----------+-----
             numeric   | 1.1
            (1 row)

    3,     select pg_typeof(nvl(to_date('2022-01-01', 'yyyy-mm-dd'), null)), nvl(to_date('2022-01-01', 'yyyy-mm-dd'), null);

              pg_typeof          |         nvl         
        -----------------------------+---------------------
         timestamp without time zone | 2022-01-01 00:00:00

c)  lightdb中nvl/nvl2 特殊情况的说明

   expr1 为 null 时返回的类型取决于数据的来源。 执行下面的例子时,根据隐式转换规则,null实际上是未知类型,所以调用的函数是nvl(text, 'any')所以函数返回text类型。

        lightdb@test=# select pg_typeof(nvl(null, 1)), nvl(null, 1);
        pg_typeof | nvl 
        -----------+-----
        text      | 1
        (1 row) 

  在下面的例子中,由于数据库中的null字段是int类型,所以函数返回一个numric类型。

        create table test_nvl (id int, func varchar(20));
        insert into test_nvl values (null, 'nvl');

        lightdb@test=# select pg_typeof(nvl(id, 1)), nvl(id, 1)  from test;
        pg_typeof | nvl 
        -----------+-----
        numeric   |   1
        (1 row)

     因为ltrim返回的类型是text,所以下面例子中实际调用的函数是nvl(text, "any"),所以函数返回的是text类型。

        lightdb@test=# select pg_typeof(nvl(ltrim(func, 'nvl'), 1)), nvl(ltrim(func, 'nvl'), 1) from test;
         pg_typeof | nvl 
        -----------+-----
         text      | 1
        (1 row)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值