当前位置:网站首页>SQL greatest() function instance detailed example

SQL greatest() function instance detailed example

2022-07-04 10:57:00 ths512

SQL GREATEST() Function example detailed example icon-default.png?t=LA92https://www.cnblogs.com/pocketbook/p/6929676.html

1、 grammar

GREATEST(expr_1, expr_2, ...expr_n) Function from expression ( Column 、 Constant 、 Calculated value )expr_1, expr_2, ... expr_n Find out the largest number in and return . In comparison ,OracIe The data type of the expression is automatically compared , With expr_1 The data type of .

2、 Use location

Procedural statements and SQL sentence

3、 Example

Example 1 :

The number ——expr_1 It's numerical . Compare by size .

All values , Take the maximum 16:

SQL> SELECT GREATEST(2, 5, 12, 3, 16, 8, 9) A FROM DUAL;

A
----------
16

Part is numerical , But the string can be based on expr_1 The data type of is converted to numeric type by implicit type conversion :

SQL> SELECT GREATEST(2, '5', 12, 3, 16, 8, 9) A FROM DUAL;

A
----------
16

Part is numerical , However, if the string cannot be implicitly converted to a numeric type, an error will be reported , Because strings A Cannot be converted to numeric :

SQL> SQL>SELECT GREATEST(2, 'A', 12, 3, 16, 8, 9) A FROM DUAL;

SQL>SELECT GREATEST(2, 'A', 12, 3, 16, 8, 9) A FROM DUAL

ORA-00900: Invalid SQL sentence

Example 2 :

Character creation ——expr_1 It's character type . Compare by initials ( If equal, compare down )

All characters , Take the maximum G:

SQL> SELECT GREATEST('A', 'B', 'C', 'D', 'E', 'F','G') A FROM DUAL;

A
-
G

All characters , The initials are equal :

SQL> SELECT GREATEST('A', 'B', 'C', 'D', 'E','GA', 'GAB') A FROM DUAL;

A
---
GAB

Part is character type , Can convert characters to non characters :

SQL> SELECT GREATEST('A', 6, 7, 5000, 'E', 'F','G') A FROM DUAL;

A
-
G

Example 3 :

Time ——expr_1 Time type .

All time types :

SQL> SELECT GREATEST(TO_DATE('2014-08-01','YYYY-MM-DD'),TO_DATE('2017-08-01','YYYY-MM-DD')) A FROM DUAL;

A
-----------
2017/8/1

Part is time type , Implicit type conversion is not allowed :

SQL> SELECT GREATEST(TO_DATE('2014-08-01','YYYY-MM-DD'),'2017-08-01') A FROM DUAL;

SELECT GREATEST(TO_DATE('2014-08-01','YYYY-MM-DD'),'2017-08-01') A FROM DUAL

ORA-01861: Text does not match format string

Example 4 :

Null value —— When expr When it's a function , Inevitably, null values will be generated

expr_1 by NULL when :

SQL> SELECT GREATEST(NULL, 'B', 'C', 'D', 'E','GA', 'GAB') A FROM DUAL;

A
-

expr_1 Not for NULL when , The rest of the expr by NULL when :

SQL> SELECT GREATEST('A', 'B', 'C', 'D', 'E',NULL, 'GAB') A FROM DUAL;

A
-

From above, we can find , as long as GREATEST Of expr There is one for NULL, Will return to NULL.

原网站

版权声明
本文为[ths512]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141413110935.html

随机推荐