По старым причинам у нас есть столбец VARCHAR2 в нашей базе данных Oracle 10, где для кодировки символов установлено значение AL32UTF8 , которое содержит некоторые значения, отличные от UTF-8. Значения всегда находятся в одном из этих наборов символов:
Я написал функцию Perl для исправления сломанных значений вне базы данных. Для значения из этого столбца базы данных он просматривает этот список кодировок и пытается преобразовать значение в UTF-8. Если преобразование завершается неудачно, он пытается выполнить следующую кодировку. Первое преобразование без ошибки — это значение, которое мы сохраняем. Теперь я хотел бы воспроизвести эту функциональность внутри базы данных, чтобы любой мог ее использовать.
Однако все, что я могу найти, это CONVERT функция, которая никогда не сбой, но вставляет заменяющий символ для символов, которые он не распознает. Поэтому, насколько я могу судить, нет способа узнать, когда конверсия не удалась.
Поэтому у меня есть два вопроса:
- Есть ли какой-нибудь существующий интерфейс, который пытается преобразовать строку в один из списков кодировок, возвращая первое, что преуспевает?
- И если нет, есть ли другой интерфейс, который указывает на отказ, если он не сможет преобразовать строку в кодировку? Если да, то я мог бы написать предыдущую функцию.
Для справки, я написал эту функцию PostgreSQL в PL/pgSQL, которая делает именно то, что мне нужно:
Мне очень хотелось бы знать, как сделать эквивалент в Oracle.
Description of the illustration convert.gif
CONVERT converts a character string from one character set to another.
The char argument is the value to be converted. It can be any of the datatypes CHAR , VARCHAR2 , NCHAR , NVARCHAR2 , CLOB , or NCLOB .
The dest_char_set argument is the name of the character set to which char is converted.
The source_char_set argument is the name of the character set in which char is stored in the database. The default value is the database character set.
The return value for CHAR and VARCHAR2 is VARCHAR2 . For NCHAR and NVARCHAR2 , it is NVARCHAR2 . For CLOB , it is CLOB , and for NCLOB , it is NCLOB .
Both the destination and source character set arguments can be either literals or columns containing the name of the character set.
For complete correspondence in character conversion, it is essential that the destination character set contains a representation of all the characters defined in the source character set. Where a character does not exist in the destination character set, a replacement character appears. Replacement characters can be defined as part of a character set definition.
Oracle discourages the use of the CONVERT function in the current Oracle Database release. The return value of CONVERT has a character datatype, so it should be either in the database character set or in the national character set, depending on the datatype. Any dest_char_set that is not one of these two character sets is unsupported. The char argument and the source_char_set have the same requirements. Therefore, the only practical use of the function is to correct data that has been stored in a wrong character set.
Values that are in neither the database nor the national character set should be processed and stored as RAW or BLOB . Procedures in the PL/SQL packages UTL_RAW and UTL_I18N —for example, UTL_RAW.CONVERT —allow limited processing of such values. Procedures accepting RAW argument in the packages UTL_FILE , UTL_TCP , UTL_HTTP , and UTL_SMTP can be used to output the processed data.
The following example illustrates character set conversion by converting a Latin-1 string to ASCII. The result is the same as importing the same string from a WE8ISO8859P1 database to a US7ASCII database.
Common character sets include:
US7ASCII: US 7-bit ASCII character set
WE8ISO8859P1: ISO 8859-1 West European 8-bit character set
EE8MSWIN1250: Microsoft Windows East European Code Page 1250
WE8MSWIN1252: Microsoft Windows West European Code Page 1252
WE8EBCDIC1047: IBM West European EBCDIC Code Page 1047
JA16SJISTILDE: Japanese Shift-JIS Character Set, compatible with MS Code Page 932
ZHT16MSWIN950: Microsoft Windows Traditional Chinese Code Page 950
UTF8: Unicode 3.0 Universal character set CESU-8 encoding form
AL32UTF8: Unicode 5.0 Universal character set UTF-8 encoding form
You can query the V$NLS_VALID_VALUES view to get a listing of valid character sets, as follows:
Oracle Database Globalization Support Guide for information on supported character sets and Oracle Database Reference for information on the V$NLS_VALID_VALUES view
I’ve got a query with a CLOB field which I want to return her value in UTF8 format. The next query works fine if the field are varchar, for example, but if it is CLOB doesn’t return a correct UTF8 string.
How can I do to return a UTF8 string from a CLOB in a query?
2 Answers 2
use dbms_lob package for it
Fixed it:
From the oracle documentation:
Oracle discourages the use of the CONVERT function in the current Oracle Database release. The return value of CONVERT has a character datatype, so it should be either in the database character set or in the national character set, depending on the datatype. Any dest_char_set that is not one of these two character sets is unsupported. …
If you need a character datatype like CLOB in a character set that differs from those the database is setup with it should be converted into a BLOB. This is where DBMS_LOB.CONVERTTOBLOB comes in.
If you need a function that returns a BLOB you have to wrap CONVERTTOBLOB into your own function. For example:
This allows queries like:
To get a list of supported values for the character set name use: