只是把你的測試在一個單一的查詢,並刪除+10
(問題是不存在),這是你有什麼:
SQL> with testCases(n) as (select 151 from dual union select 51 from dual)
2 select n,
3 substr(to_char(n, '099'),1,3) as substr_3,
4 substr(n,1,2) as substr_3
5 from testCases;
N SUBSTR_3 SUBSTR_3
---------- ------------ --------
51 05 51
151 15 15
我相信是什麼讓你期望一個不同的結果是to_char
應該做什麼;澄清一下,看看下面的結果:
SQL> with testCases(n) as (select 151 from dual union select 51 from dual)
2 select n,
3 '|' || to_char(n, '099') || '|' as to_char
4 from testCases;
N TO_CHA
---------- ------
51 | 051|
151 | 151|
這裏可以看到,to_char
添加一個空格所得到的字符串;這會使你的子字符串邏輯失效,給你意想不到的結果。 這種行爲清楚地解釋here:
額外的前導空格是潛在的減號。要刪除 空間,您可以在格式
事實上使用FM,如果編輯格式掩碼,您有
SQL> with testCases(n) as (select 151 from dual union select 51 from dual)
2 select n,
3 '|' || to_char(n, 'FM099') || '|' as to_char
4 from testCases;
N TO_CHA
---------- ------
51 |051|
151 |151|
和測試用例成爲:
SQL> with testCases(n) as (select 151 from dual union select 51 from dual)
2 select n,
3 substr(to_char(n, 'FM099'),1,3) as substr_3,
4 substr(n,1,2) as substr_2
5 from testCases ;
N SUBSTR_3 SUBSTR_2
---------- ------------ --------
51 051 51
151 151 15
嘗試'選擇'|' || to_char(151,'099')|| '|' from dual',然後將格式掩碼編輯爲'FM099'';這應該可以幫助你找到問題 – Aleksej