Oracle study notes 007-2 (single function: null correlation function, the codec function, a nested function)

Here Insert Picture Description

Null correlation function

Function Introduction
function effect
NVL(x1,x2) If x1 is NULL, then the function displays the value of x2
NVL2(x1,x2,x3) If x1 is NULL, then the function displays the value of x3; is not NULL, the display value of x2
NULLIF(x1,x2) If x1 = x2 return NULL; if not, etc., a value of the first expression is returned;
COALSECE(x1,x2,x3,…xn) If the whole is NULL, the function value is NULL; if one is not NULL, then displays the first entry is not empty; three if not empty, the frontmost one will be displayed;
Case code demonstrates
--两个参数都不为空,返回第一个参数
SQL> SELECT NVL(0,1) FROM DUAL;

  NVL(0,1)
----------
         0
 
 --第一个参数为空,则返回第二个参数
SQL> SELECT NVL(NULL,1) FROM DUAL;

NVL(NULL,1)
-----------
          1
          
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

-- 第一个参数不为空值,返回第二个
SQL> SELECT NVL2(1,2,3) FROM DUAL;

NVL2(1,2,3)
-----------
          2

--第一个参数为空,则返回第三个
SQL> SELECT NVL2(NULL,2,3) FROM DUAL;

NVL2(NULL,2,3)
--------------
             3

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
--两个参数相等返回第一个
SQL> SELECT NULLIF(1,2) FROM DUAL;

NULLIF(1,2)
-----------
          1

--两个参数不相等,返回空
SQL> SELECT NULLIF(2,2) FROM DUAL;

NULLIF(2,2)
-----------

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

--参数全部为空,返回空
SQL> SELECT COALESCE(NULL,NULL,NULL) FROM DUAL;

C
----------

--有一个不为空,返回第一个非空值
SQL> SELECT COALESCE(NULL,2,NULL) FROM DUAL;

COALESCE(NULL,2,NULL)
---------------------
                    2
                    

--都不为空则返回第一个值
SQL> SELECT COALESCE(1,2,3) FROM DUAL;

COALESCE(1,2,3)
---------------
              1

Codec function

Function Code Description
  1. DECODE

    • if -elif condition judgment logic is similar in function DECODE - else

    • The DECODE (condition 1 condition, returns the value 1, the condition 2, returns the value 2, ... condition n, the return value n, others)

      Explain what function:
      the IF condition THEN = condition. 1
           the RETURN (Return value. 1)
          
      ELSIF condition THEN = Condition 2
           the RETURN (Return Value 2)
           ...
      the ELSE
           the RETURN (other)
      the END the IF


SQL> SELECT DECODE(99,9,'不相等',99,'相等','no') FROM DUAL;

DECODE(99,9,
------------
相等
  1. DUMP

    • DUMP(expr, [return_fmt] [start_position] [length]])

    • There are four basic parameters, but at least when you can not fill, direct return NULL, the other three parameters also have their default values.

      Parameter Details
      expr: This parameter is the operation data (or other digital strings, the value of each type may be)

      return_fmt: refers to the format of the return parameters, there are five kinds of usage:

      • 1) 8: returns the result to the binary value 8

      • 2) 10: return the value of 10 decimal results (default)

      • 3) 16: returns the result in hexadecimal values

      • 4) 17: returns the result in the form of a single character value

      • 5) 1000: 1000 plus four or more, plus the return value indicates the current character set

      start_position: start character position returned

      length: the need to return to the character length

Case code demonstrates
-- 不传入参数
SQL> SELECT DUMP('abc') FROM DUAL;

DUMP('ABC')
--------------------------------------------
Typ=96 Len=3: 97,98,99


--传入三个参数的时候,最后一个长度参数不传。则默认后边剩余的所有字符
SQL> SELECT DUMP('abcdef',1000,2) FROM DUAL;

DUMP('ABCDEF',1000,2)
----------------------------------------------
Typ=96 Len=6: b,c,d,e,f


--全部参数传入
SQL> SELECT DUMP('abcdef',1000,2,4) FROM DUAL;

DUMP('ABCDEF',1000,2,4)
------------------------------------------
Typ=96 Len=6: b,c,d,e


--在返回值中加上字符集
SQL> SELECT DUMP('abcdef',1016,2,4) FROM DUAL;

DUMP('ABCDEF',1016,2,4)
--------------------------------------------------------------------------------
Typ=96 Len=6 CharacterSet=AL32UTF8: 62,63,64,65

Nested functions

Function Introduction
  • When a function is defined in the interior of another function when the return value of a function that is an argument to a function of time on the formation of nested function.
  • One-way functions can be nested to any level
  • Nested function execution order is from the inside to the outside
Case presentation
-- 利用嵌套函数,查询二十年后的今天,返回日期,星期
SQL> SELECT TO_CHAR( ADD_MONTHS(TO_DATE(SYSDATE),12*20),'YYYY-mm-dd day') BIRTHDAY_182 FROM DUAL;

BIRTHDAY_182
----------------------------------------------
2040-03-16 星期二


While studying records, if any deficiencies welcome message pointing ...

Published 63 original articles · won praise 1 · views 2021

Guess you like

Origin blog.csdn.net/qq_45061361/article/details/104892282