當前位置:首頁 » 編程語言 » sql函數coalesce
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql函數coalesce

發布時間: 2022-01-30 16:45:29

A. 求SQL語言中COALESCE字元函數的用法

功能:返回其參數中的第一個非空表達式,當你要在n個欄位中選取某一個非空值
可以用它,比如下面語句
select Coalesce(null,null,1,2,null)union
select Coalesce(null,11,12,13,null)union
select Coalesce(111,112,113,114,null)

返回結果:
1
11
111

B. SQL中 nvl()、coalesce()、decode()這三個函數,如果只是判斷非空的話,哪一個效率相比較高

nvl只支持2個參數,這是oracle特有的
coalesce支持不定參數,coalesce(bonus,0,1)應該寫成coalesce(bonus,0),最後1個不用寫,這是ansi標准函數,在多次外連接時尤其有用,譬如 t1 full join t2 on t1.id=t2.id full join t3 on coalesce(t1.id,t2.id)=t3.id full join t4 on coalesce(t1.id,t2.id,t3.id)=t4.id
decode判斷不了非空

C. SQL中 nvl()coalesce()decode()這三個函數是不是遞進包含關系

nvl(bonus,0) 意思是 如果 bonus is null , 那麼返回 0, 否則返回 bonus

coalesce(bonus,0,1) 意思是 返回 參數列表中, 第一個非 空的數據。
也就是相當於, 如果 bonus is null , 那麼返回 0, 否則返回 bonus。
這里的最後一個參數 1, 目測是打醬油的。

coalesce(bonus,null,0) 意思是 返回 參數列表中, 第一個非 空的數據。
也就是相當於, 如果 bonus is null , 那麼第2個參數還是 null, 最後返回第3個參數 0

decode(name,『apple』,0) 意思是, 如果 name = 'apple' 那麼返回 0
否則的話 , 就是返回 null 了。

D. SQL Server ISNULL函數和Coalesce函數替換空值的區別

SELECT COALESCE('',0)結果0
SELECT COALESCE(' ',0) 結果0
SELECT COALESCE(null,0) 結果0
SELECT COALESCE(123,0) 結果123
SELECT ISNULL('',0) 結果''
SELECT ISNULL(null,0) 結果0
SELECT ISNULL(123,0)結果123
由結果結果可以看出COALESCE函數對於空值處理和NULL值都起作用。

E. COALESCE(MAX(meta_id),0)+1,請問這個SQL語句是什麼意思

表達式:COALESCE(MAX(meta_id),0)+1
其意思為選取欄位"meta_id"的最大值+1,如果該最大值為Null(空值),則將空值替換為0,然後+1

COALESCE函數簡要說明:
COALESCE (expression_1, expression_2, ...,expression_n)依次參考各參數表達式,遇到非null值即停止並返回該值。如果所有的表達式都是空值,最終將返回一個空值。

F. 函數VALUE和COALESCE到底有沒有區別

Purpose

NVL lets you replace null (returned as a blank) with a string in the results of a query. If expr1 is null, then NVL returns expr2. If expr1 is not null, then NVLreturns expr1.

The arguments expr1 and expr2 can have any data type. If their data types are different, then Oracle Database implicitly converts one to the other. If they cannot be converted implicitly, then the database returns an error. The implicit conversion is implemented as follows:

• If expr1 is character data, then Oracle Database converts expr2 to the data type of expr1 before comparing them and returns VARCHAR2 in the character set of expr1.

• If expr1 is numeric, 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.

Examples

SQL> select * from scott.procts;

LIST_PRICE MIN_PRICE

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

10000 8000

20000

30000 30000

SQL> select min_price,nvl(min_price,0) from scott.procts;

MIN_PRICE NVL(MIN_PRICE,0)

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

8000 8000

0

30000 30000

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

nvl2用法為nvl2(expr1,expr2,expr3),其作用是判斷expr1是否為null,若不為null,返回expr2,為空返回expr3。

nvl(expr1,expr2)等同於nvl2(expr1,expr1,expr2)。

官方文檔用法解釋如下:

NVL2

Syntax

De.ion of nvl2.gif follows

Purpose

NVL2 lets you determine the value returned by a query based on whether a specified expression is null or not null. If expr1 is not null, then NVL2 returnsexpr2. If expr1 is null, then NVL2 returns expr3.

The argument expr1 can have any data type. The arguments expr2 and expr3 can have any data types except LONG.

If the data types of expr2 and expr3 are different, then Oracle Database implicitly converts one to the other. If they cannot be converted implicitly, then the database returns an error. If expr2 is character or numeric data, then the implicit conversion is implemented as follows:

• If expr2 is character data, then Oracle Database converts expr3 to the data type of expr2 before returning a value unless expr3 is a null constant. In that case, a data type conversion is not necessary, and the database returns VARCHAR2 in the character set of expr2.



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.

Examples

SQL> select list_price,www.hbbz08.com min_price,nvl2(min_price,min_price,list_price) nv2_m from scott.procts;

LIST_PRICE MIN_PRICE NV2_M

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

10000 8000 8000

20000 20000

30000 30000 30000

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

nullif用法為nullif(expr1,expr2),其作用是判斷expr1與expr2是否相等,若相等則返回null,否則返回expr1。

官方文檔用法解釋如下:

NULLIF

Syntax

De.ion of nullif.gif follows

Purpose

NULLIF compares expr1 and expr2. If they are equal, then the function returns null. If they are not equal, then the function returns expr1. You cannot specify the literal NULL for expr1.

If both arguments are numeric data types, then Oracle Database determines the argument with the higher numeric precedence, implicitly converts the other argument to that data type, and returns that data type. If the arguments are not numeric, then they must be of the same data type, or Oracle returns an error.

The NULLIF function is logically equivalent to the following CASE expression:
CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END

Examples

SQL> select list_price,min_price,nullif(list_price,min_price) from scott.procts;

LIST_PRICE MIN_PRICE NULLIF(LIST_PRICE,MIN_PRICE)

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

10000 8000 10000

20000 20000

30000 30000

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

coalesce用法為coalesce(expr1,expr2……exprn),其作用是在expr1,expr2……exprn這列表達式中查找第一個不為null的值且返回該值。如果都為null,則返回null。

官方文檔用法解釋如下:

COALESCE

Syntax

De.ion of coalesce.gif follows

Purpose

COALESCE returns the first non-null expr in the expression list. You must specify at least two expressions. If all occurrences of expr evaluate to null, then the function returns null.

Oracle Database uses short-circuit evaluation. The database evaluates each expr value and determines whether it is NULL, rather than evaluating all of theexpr values before determining whether any of them is NULL.

If all occurrences of expr are numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type, then Oracle Database determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

This function is a generalization of the NVL function.

You can also use COALESCE as a variety of the CASE expression. For example,
COALESCE(expr1, expr2)

is equivalent to:
CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END

Similarly,
COALESCE(expr1, expr2, ..., exprn)

where n >= 3, is equivalent to:
CASE WHEN expr1 IS NOT NULL THEN expr1
ELSE COALESCE (expr2, ..., exprn) END

Examples

SQL> select list_price,min_price,coalesce(list_price,min_price) from scott.procts;

LIST_PRICE MIN_PRICE COALESCE(LIST_PRICE,MIN_PRICE)

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

10000 8000 10000

20000 20000

30000 30000 30000

到此,nvl、nvl2、nullif、coalesce四個函數用法我們都了解了,看似很簡單,但實際結合應用起來,就容易犯混,如下例子:

examples:

Examine the data in the LIST_PRICE and MIN_PRICE columns of the PRODUCTS table:

LIST_PRICE MIN_PRICE

10000 8000

20000

30000 30000

Which two expressions give the same output? (Choose two.)

A. NVL(NULLIF(list_price, min_price), 0)

B. NVL(COALESCE(list_price, min_price), 0)

C. NVL2(COALESCE(list_price, min_price), min_price, 0)

D. COALESCE(NVL2(list_price, list_price, min_price), 0)

Answer: BD

乍看你呢很快給出答案么,下面給出答案解析。

A. NVL(NULLIF(list_price, min_price), 0) 查詢結果如下:

SQL> select NVL(NULLIF(list_price, min_price), 0) from scott.procts;

NVL(NULLIF(LIST_PRICE,MIN_PRICE),0)

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

10000

20000

0

B. NVL(COALESCE(list_price, min_price), 0) 查詢結果如下:

SQL> select NVL(COALESCE(list_price, min_price), 0) from scott.procts;

NVL(COALESCE(LIST_PRICE,MIN_PRICE),0)

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

10000

20000

30000

C. NVL2(COALESCE(list_price, min_price), min_price, 0)查詢結果如下:

SQL> select NVL2(COALESCE(list_price, min_price), min_price, 0) from scott.procts;

NVL2(COALESCE(LIST_PRICE,MIN_PRICE),MIN_PRICE,0)

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

8000

30000

D. COALESCE(NVL2(list_price, list_price, min_price), 0) 查詢結果如下:

SQL> select COALESCE(NVL2(list_price, list_price, min_price), 0) from scott.procts;

COALESCE(NVL2(LIST_PRICE,LIST_PRICE,MIN_PRICE),0)

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

10000

20000

30000

由此可見,選BD

G. oracle存儲過程中帶空值的輸入怎麼寫COALESCE()函數用錯了嗎

空值 ,你直接用NVL不就行了.
open cusers for select * from C_USERS where logintime >= NVL(start_time,logintime)
and logintime <= NVL(end_time,logintime)
and username = NVL(name,username)
and truename = NVL(tname,truename);

H. COALESCE 是否是SQL標准函數

在oracle中不是,
server中,沒測試。
資料庫系統函數,是可以自己查看到的。

I. SQL語句:COALESCE(t1.CONTENT,'') <>'' 為什麼不等於 t1.CONTENT<>NULL我覺得是一樣的。。。

null 在資料庫中 表示 不可知

你不能 用 = null 、 <> null 等等 進行判斷。

判斷的結果 同樣是: 不可知(不確定)

所以所有的返回結果都是 false

可以 t1.CONTENT is not NULL 、 t1.CONTENT is NULL 進行判斷

J. 求SQL語言中COALESCE字元函數的用法!!!!!

功能:返回其參數中的第一個非空表達式,當你要在n個欄位中選取某一個非空值
可以用它,比如下面語句
select Coalesce(null,null,1,2,null)union
select Coalesce(null,11,12,13,null)union
select Coalesce(111,112,113,114,null)

返回結果:
1
11
111