案例來源: PTT

輸入 select substr(atable,1,7) from table A;
的結果為
 0001236
 0000782
 0000107
 0000227
 0001489
 我使用想讓輸出的結果為
 1236
 782
 107
 227
 1489

 

方法: 透過,instr+substr

*********************

select substr(atable,instr(atable,'0',-1)+1,10) from (
select substr(atable,1,7) atable from  table A);

*********************

說明:

找出右到左0出現的位置,再將0之後的數字取出來

instr(atable,'0',-1)+1     其中的-1 表示右到左搜尋 , 0表示找到0的位置,重點是為何+1

因為 如果沒+1 那顯示出來的字串為 01236 (以第一個為例) 他會連0一起輸出,所以使用+1 使0的位置在加1 因此

搭配substr就可以輸出1236

 

*instr 是找字串的位置

*substr 是擷取字串

 

 

 

 

 

文章標籤
全站熱搜
創作者介紹
創作者 to52016 的頭像
to52016

Eason [資料科學//Python學習/資料庫] & [拍片&剪片]

to52016 發表在 痞客邦 留言(0) 人氣(28)