1 概述
2 示例
-- 类型 比较大小的依据
-- 数值 数值大小
-- 字符串 ascii大小 select ascii('a') from dual;
-- 时间 时间大小
-- NULL、'' 只要有 null,都是 null
select greatest(1, 2, 3) max_number,
least(1, 2, 3) min_number,
greatest('a', 'b', 'c') max_string,
least('a', 'b', 'c') min_string,
greatest(sysdate, sysdate + 1, sysdate + 2) max_date,
least(sysdate, sysdate + 1, sysdate + 2) min_date,
greatest(1, null, 2) max_null,
least(1, '', 2) min_null
from dual;
3 扩展
3.1 获取非空列值
/*
-- 依据:进行判空操作 或 给默认值
select nvl('', '空') a,
nvl2('', '非空', '空') b,
-- 获取第一个非空的值
coalesce('', '', '', '非空') c -- N 列
from dual;
*/
with t_temp as (
select '' col1, 'a' col2, 'b' col3 from dual
)
select greatest(coalesce(tt.col1, tt.col2, tt.col3),
coalesce(tt.col2, tt.col3, tt.col1),
coalesce(tt.col3, tt.col1, tt.col2)) 最大值
from t_temp tt;
3.2 隐式类型转换
- 结论:表达式的数据类型以
第一个 exp
为准
create or replace view scott.test_date_type as
select greatest(1, '2') string_to_number, -- number
least('1', 2) number_to_string -- varchar2
from dual
;
select t.column_name,
t.data_type
from all_tab_columns t
where t.table_name = upper('test_date_type');
输出结果: