網站首頁 編程語言 正文
1.decode函數的兩種形式
第一種形式
含義解釋:
decode(條件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)
該函數的含義如下:
IF 條件=值1 THEN RETURN(翻譯值1) ELSIF 條件=值2 THEN RETURN(翻譯值2) ...... ELSIF 條件=值n THEN RETURN(翻譯值n) ELSE RETURN(缺省值) END IF
第二種形式
decode(字段或字段的運算,值1,值2,值3)
這個函數運行的結果是,當字段或字段的運算的值等于值1時,該函數返回值2,否則返回值3
當然值1,值2,值3也可以是表達式,這個函數使得某些sql語句簡單了許多
sign()函數
解釋:
sign()函數根據某個值是0、正數還是負數,分別返回0、1、-1
用法示例:
select id,sign(id-2) from t_decode;
截圖效果:
lpad()函數
在字段id前邊補字段0 長度為2
select lpad(id,2,0) from t_decode;
2.decode的一些工作常用思路總結
a.準備測試數據
創建測試表t_decode,并插入測試數據
//創建表,插入數據,查詢 create table t_decode( id integer, name varchar2(10) ); //插入數據 insert into t_decode values (1,'a'); insert into t_decode values (2,'b'); insert into t_decode values (3,'c'); insert into t_decode values (4,'a');
數據樣式
b.第一種形式decode函數的常用思路
1. 簡單使用:判斷字符串
select id,name, decode(id,1,'第一個',2,'第二個',3,'第三個','沒有') new_id from t_decode;
2.使用decode函數分段
判斷表中id大小并根據大小劃分不同范圍
(-,2) low
[2,4) mid
[4,-] high
select id,name, decode(sign(id - 4),1,'high id',0,'high id','-1', decode(sign(id - 2),1,'mid id',0,'mid id',-1,'low id')) from t_decode;
c.第二種形式decode函數的常用思路
1. 比較大小
-- 比較大小 select decode(sign(100-90),-1,100,90) from dual;
2. 使用表達式來搜索字符串
判斷name中是否含有a?
select id,name,decode(instr(name,'a'),0,'不含有a','含有a') as info from t_decode;
3. 實現行列轉換
注意:decode相當于:case when then else end語句
select sum(decode(name,'a',id,0)) id_1, sum(decode(name,'b',id,0)) id_2, sum(decode(name,'c',id,0)) id_3 from t_decode;
添加測試:
select decode(name,'a',id,0) id_1, decode(name,'b',id,0) id_2, decode(name,'c',id,0) id_3 from t_decode; -- 相等于:case when then else end select case name when 'a' then id else 0 end as id_1, case name when 'b' then id else 0 end as id_2, case name when 'c' then id else 0 end as id_3 from t_decode;
4. 結合Lpad函數,如何使主鍵的值自動加1并在前面補0
結合Lpad函數,如何使主鍵的值自動加1并在前面補0 select lpad(decode(count(id),0,1,max(to_number(id)+1)),14,'0') new_id from t_decode;
拆分詳細講解:
select * from t_decode
select lpad(id,2,0) from t_decode
select to_number(id) from t_decode;
select max(to_number(id)+1) from t_decode;
select decode(count(id),0,1,max(to_number(id)+1)) from t_decode;
select lpad(decode(count(id),0,1,max(to_number(id)+1)),14,‘0') new_id from t_decode;
原文鏈接:https://blog.csdn.net/qichangjian/article/details/88975499
相關推薦
- 2022-10-15 conda虛擬環境使用pip下載包到當前環境的兩種方法_python
- 2022-10-03 C++模擬實現vector流程詳解_C 語言
- 2022-08-24 使用chrome控制臺作為.Net的日志查看器_實用技巧
- 2022-07-17 SQL?Server中使用表變量和臨時表_MsSql
- 2022-03-14 【golang/方法記錄】有序列表的簡易實現
- 2022-09-29 Python模塊域名dnspython解析_python
- 2022-03-24 Android使用Span打造豐富多彩的文本詳解_Android
- 2022-05-24 Golang?錯誤捕獲Panic與Recover的使用_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支