Conversion functions

Updated at:

PolarDB for Oracle supports the following conversion functions, which are compatible with Oracle SQL syntax.

TO_YMINTERVAL

Converts a string to an INTERVAL YEAR TO MONTH value.

Syntax

TO_YMINTERVAL(char)

Parameters

ParameterDescription
charThe string to convert. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2.

Return value

INTERVAL YEAR TO MONTH

Examples

SQL interval format (compatible with the SQL standard):

SELECT to_yminterval('2016-1') FROM DUAL;

Output:

 to_yminterval
---------------
 +2016-01

TO_DSINTERVAL

Converts a string to an INTERVAL DAY TO SECOND value.

Syntax

TO_DSINTERVAL(char[, nlsparam])

Parameters

ParameterDescription
charThe string to convert. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2.
nlsparamOptional. Specifies NLS_NUMERIC_CHARACTERS in the format NLS_NUMERIC_CHARACTERS="dg", where d is the decimal character and g is the group delimiter. Neither character can be a space.

Return value

INTERVAL DAY TO SECOND

Examples

SQL interval format (compatible with the SQL standard):

SELECT to_dsinterval('1 1:1:00') FROM DUAL;

Output:

 to_dsinterval
---------------
 +01 01:01:00

TO_TIMESTAMP_TZ

Converts a string to a TIMESTAMP WITH TIME ZONE value.

Syntax

TO_TIMESTAMP_TZ(char[, fmt][, 'nlsparam'])

Parameters

ParameterDescription
charThe string to convert. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2.
fmtThe format of the returned TIMESTAMP WITH TIME ZONE value.
nlsparamOptional. The language used for month and day names in the output.

Return value

TIMESTAMP WITH TIME ZONE

NUMTODSINTERVAL

Converts a numeric value to an INTERVAL DAY TO SECOND value.

interval_unit is case-insensitive. Leading and trailing parentheses () in the value are ignored. The default precision of the return value is 9.

Syntax

NUMTODSINTERVAL(n, interval_unit)

Parameters

ParameterDescription
nA NUMBER value, or an expression that implicitly converts to NUMBER.
interval_unitThe unit for n. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2. Valid values: DAY, HOUR, MINUTE, SECOND.

Return value

INTERVAL DAY TO SECOND

Examples

Basic conversion:

SELECT numtodsinterval('3.123456789', 'hour') FROM DUAL;

Output:

   numtodsinterval
--------------------
 +00 03:07:24.44444

NUMTOYMINTERVAL

Converts a numeric value to an INTERVAL YEAR TO MONTH value.

interval_unit is case-insensitive. Leading and trailing parentheses () in the value are ignored. The default precision of the return value is 9.

Syntax

NUMTOYMINTERVAL(n, interval_unit)

Parameters

ParameterDescription
nA NUMBER value, or an expression that implicitly converts to NUMBER.
interval_unitThe unit for n. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2. Valid values: YEAR, MONTH.

Return value

INTERVAL YEAR TO MONTH

Example

SELECT numtoyminterval('3.123456789', 'year') FROM DUAL;

Output:

 numtoyminterval
-----------------
 +03-01

UNISTR

Converts a string or expression to a value in the national character set. The national character set supports the AL16, UTF16, or UTF8 encoding format.

Unicode code points are specified in \xxxx format, where xxxx is the hexadecimal value in UCS-2 encoding. Supplementary characters outside the Basic Multilingual Plane use surrogate pairs: the high surrogate ranges from U+D800 to U+DBFF, and the low surrogate from U+DC00 to U+DFFF. To include a literal backslash, use \\.

Syntax

UNISTR(string)

Parameters

ParameterDescription
stringThe string to convert.

Return value

STRING

Example

SELECT UNISTR('abc\00e5\00f1\00f6') FROM DUAL;

Output:

 unistr
--------
 abcåñö

Related function: ASCIISTR converts a string to ASCII, replacing non-ASCII characters with \xxxx escape sequences.

TO_SINGLE_BYTE

Converts multi-byte characters to their single-byte equivalents.

Multi-byte characters with no single-byte equivalent remain as multi-byte characters in the output. This function only works if the database character set contains both single-byte and multi-byte characters.

CLOB values are not directly supported, but can be passed via implicit data type conversion.

Syntax

TO_SINGLE_BYTE(char)

Parameters

ParameterDescription
charThe value to convert. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2.

Return value

Same data type as the input char.

Example

SELECT to_single_byte('123$test') FROM DUAL;

Output:

 to_single_byte
----------------
 123$test

TO_MULTI_BYTE

Converts single-byte characters to their multi-byte equivalents.

Single-byte characters with no multi-byte equivalent remain as single-byte characters in the output. This function only works if the database character set contains both single-byte and multi-byte characters.

CLOB values are not directly supported, but can be passed via implicit data type conversion.

Syntax

TO_MULTI_BYTE(char)

Parameters

ParameterDescription
charThe value to convert. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2.

Return value

Same data type as the input char.

Example

SELECT to_multi_byte('Hello'::text) FROM DUAL;

Output:

 to_multi_byte
---------------
 Hello

TO_CLOB

Converts an NCLOB column or string to a CLOB value.

In a PL/SQL package, you can use the TO_CLOB function to convert values of the following data types to values of the CLOB or NCLOB data types: RAW, CHAR, VARCHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, and NCLOB.

Syntax

TO_CLOB(lob_column | char)

Parameters

ParameterDescription
lob_columnA column of the NCLOB data type.
charA string value. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, NCLOB.

Return value

CLOB

Example

CREATE TABLE Employee_to_clob (
  ID         VARCHAR2(4 BYTE),
  First_Name VARCHAR2(10 BYTE)
);
INSERT INTO Employee_to_clob (ID, First_Name) VALUES ('01', 'Jason');

SELECT to_clob(first_name) FROM Employee_to_clob;

Output:

 to_clob
---------
 Jason

RAWTOHEX

Converts a RAW value to a VARCHAR2 string in hexadecimal format. Each byte is represented as two hexadecimal digits.

As a SQL built-in function, RAWTOHEX accepts any scalar data type except LONG, LONG RAW, CLOB, NCLOB, BLOB, and BFILE. When the input is not of the RAW type, the function recasts the data to RAW using the same number of bytes — the data itself is not modified.

Syntax

RAWTOHEX(raw)

Parameters

ParameterDescription
rawThe value to convert. Accepted data types: any scalar type except LONG, LONG RAW, CLOB, NCLOB, BLOB, and BFILE.

Return value

VARCHAR2 in hexadecimal format.

Example

SELECT rawtohex('hello world'::varchar) FROM DUAL;

Output:

        rawtohex
------------------------
 68656c6c6f20776f726c64

Related function: HEXTORAW is the inverse — it converts a hexadecimal string back to RAW.

HEXTORAW

Converts a hexadecimal string to a RAW value.

CLOB values are not directly supported, but can be passed via implicit data type conversion.

Syntax

HEXTORAW(char)

Parameters

ParameterDescription
charThe hexadecimal string to convert. Accepted data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2.

Return value

RAW

Example

SELECT hextoraw(rawtohex('hello world'::bpchar)) FROM DUAL;

Output:

          hextoraw
--------------------------
 \x68656c6c6f20776f726c64

Related function: RAWTOHEX is the inverse — it converts a RAW value to a hexadecimal string.

ASCIISTR

Converts a string in any character set to an ASCII string in the database character set. Non-ASCII characters are replaced with \xxxx escape sequences, where xxxx is the UTF-16 code unit.

Syntax

ASCIISTR(char)

Parameters

ParameterDescription
charA string in any character set, or an expression that resolves to a string.

Return value

An ASCII string in the database character set.

Example

SELECT ASCIISTR('WÈÐńėśĎày') FROM DUAL;

Output:

                asciistr
---------------------------------------
 W\00C8\00D0\0144\0117\015B\010E\00E0y

Related function: UNISTR is the inverse — it converts Unicode escape sequences back to characters in the national character set.