Oracle 中文字元及中文亂碼判斷

xz43發表於2011-02-23

Oracle裡面如何識別儲存欄位裡面是否存在中文?

方法一:

    使用length和lengthb,如果中文的話length是一個字元,但是lengthb是兩個位元組。但是這個方法不適合資料庫字符集非gbk的情況。

select * from t where length(c1) != lengthb(c1);

方法二:

    使用asciistr函式得出是否欄位裡面包含“\”,因為當中文字元轉換為ascii後,變成“\FFFD\FFFD”,但是需要注意一個特殊字元“\”,當它出現的時候轉換後的碼為“\005C”

SQL> select asciistr('/\)(^&*~中文字元') from dual;

ASCIISTR('/\)(^&*~中文字元')
-----------------------------------------------------------
/\005C)(

Oracle裡面如何判斷procedure等指令碼里面是否包含中文亂碼?

還是透過 asciistr 函式來實現了,如下:

SQL> select name, type
  from user_source
 where asciistr(text) like '%\FFFD%'
 group by name, type;  2    3    4 

NAME                   TYPE
---------------------- ------------
AGE_STAT_SUPERIOR      PROCEDURE
AUTOCREATE_SOURCE_DJ  PROCEDURE
AUTOCREATE__SOURCE_SJ  PROCEDURE

然後透過user_source加上上面的查詢結果中的值,找得詳細指令碼

select text from user_source where name=upper('AGE_STAT_SUPERIOR');

附錄:

The Oracle ASCIISTR Function 

Definition:
The Oracle ASCIISTR function takes a string (or an expression that resolves to a string), in any and returns an ASCII version of the string in the current database character set.

The ASCIISTR function is useful for taking strings with accented characters (such as strings containing unicode characters) and converting them to an "ASCII-safe" format.

Example Usage:

SELECT ASCIISTR('The letter Å will be converted.') asc_str
FROM dual;



This example returns the following string:

The letter \00C5 will be converted.

 

Applies To:

  • Oracle 9i, Oracle 10g, Oracle 11g

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9399028/viewspace-687789/,如需轉載,請註明出處,否則將追究法律責任。

相關文章