這是什麼可以被認爲是一個錯誤的組合 - 但實際上可能是至關重要的到Oracle如何使用nlssort
- 以及您的客戶端如何處理字符串。在SQL Developer中,例如,當它作爲語句或腳本時,這似乎按預期工作,但我無法從工作表或查詢結果網格中複製和粘貼結果。
使用dump()
功能,你可以看到,彌補結果的字符:
select dump(utl_raw.cast_to_varchar2((nlssort('"' || CITY_NAME || ', ' || STATE_CODE || '"',
'nls_sort=binary_ai'))) || ' test', 1016) as dumped_result
from (select 'New York' as CITY_NAME, 'NY' as STATE_CODE from dual);
DUMPED_RESULT
----------------------------------------------------------------------------------------------------
Typ=1 Len=20 CharacterSet=AL32UTF8: 22,6e,65,77,20,79,6f,72,6b,2c,20,6e,79,22,0,20,74,65,73,74
^
我標誌着^
標記來突出顯示在輸出0
,投nlssort()
結果與之間您正在添加test
。還是有點更清楚而不串聯:
select dump(utl_raw.cast_to_varchar2(nlssort('ABC')), 1016) as dumped_result
from (select 'New York' as CITY_NAME, 'NY' as STATE_CODE from dual);
DUMPED_RESULT
----------------------------------------------------------------------------------------------------
Typ=1 Len=4 CharacterSet=AL32UTF8: 41,42,43,0
甚至更好:
select dump(nlssort('ABC'), 1016) as dumped_result from dual;
DUMPED_RESULT
----------------------------------------------------------------------------------------------------
Typ=23 Len=4: 41,42,43,0
的nlssort()
呼叫添加空字節,顯示爲轉儲輸出0
,到結果。你的客戶端看到,作爲字符串的結尾,所以即使Oracle實際上串聯字符串,你也看不到結果。
您可以刪除null(例如,與rtrim()
,在連接前:
select rtrim(utl_raw.cast_to_varchar2(nlssort('"' || CITY_NAME || ', ' || STATE_CODE || '"',
'nls_sort=binary_ai')), chr(0)) || ' test' as result
from (select 'New York' as CITY_NAME, 'NY' as STATE_CODE from dual);
RESULT
----------------------------------------------------------------------------------------------------
"new york, ny" test
或與較短的原始字符串:
select dump(rtrim(utl_raw.cast_to_varchar2(nlssort('ABC')), chr(0)), 1016) as dumped_result
from (select 'New York' as CITY_NAME, 'NY' as STATE_CODE from dual);
DUMPED_RESULT
----------------------------------------------------------------------------------------------------
Typ=1 Len=3 CharacterSet=AL32UTF8: 41,42,43
,你可以看到現在有沒有尾隨空字符。
這可能是由於一個「錯誤」導致'nlssort'添加一個字符串終止符; [here](http://stackoverflow.com/questions/19774073/strange-behavior-of-length-command-oracle)你可以找到更多的東西。例如,如果您嘗試從雙重'選擇轉儲(utl_raw.cast_to_varchar2((nlssort('something','nls_sort = binary_ai')))),您會看到在字符串末尾添加了「0」字符,所以無論你在這個char之後添加的字符串,都會被忽略,因爲它在字符串結尾之後。 – Aleksej
@Aleksej - 我認爲這聽起來很熟悉,但我找不到之前看到它的地方* 8-)什麼是在null之後並不總是完全忽略,整個串聯的字符串顯示在SQL Developer中,例如,但即使真的(不能複製/粘貼),它也不處於可用狀態。 –