In Oracle database, sometimes we need to convert data from one data type to another. The most common conversion is to convert other data types into character types. In this case, we can use some built-in functions provided by Oracle to achieve data type conversion. Below we will introduce in detail several commonly used character conversion functions in Oracle.
TO_CHAR() function converts data types such as numbers, dates, and times into character types. Its syntax is as follows:
TO_CHAR(expression [, format_mask] [, nls_language])
Among them, expression represents the expression to be converted, which can be a data type such as number, date, time, etc.; format_mask represents an optional output format template, used to specify the output format; nls_language Represents an optional locale that specifies the character set for conversion. If format_mask and nls_language are omitted, the local settings of the database are used by default.
The following are some examples of using the TO_CHAR() function:
SELECT TO_CHAR(12345.6789) FROM dual; -- 输出结果为'12345.6789'
SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') FROM dual; -- 输出结果为'2022-03-14'
SELECT TO_CHAR(TIMESTAMP'2022-03-14 00:00:00.000000000','YYYY-MM-DD HH24:MI:SS') FROM dual; -- 输出结果为'2022-03-14 00:00:00'
CAST The () function is used to convert one data type to another data type, including converting other data types into character types. Its syntax is as follows:
CAST(expression AS data_type)
Among them, expression represents the expression to be converted, and data_type represents the data type to be converted into. Here are some examples of using the CAST() function:
SELECT CAST(12345.6789 AS CHAR(10)) FROM dual; -- 输出结果为' 12345.68'
SELECT CAST(SYSDATE AS VARCHAR2(20)) FROM dual; -- 输出结果为'14-MAR-22'
SELECT CAST(TIMESTAMP'2022-03-14 00:00:00.000000000' AS VARCHAR2(30)) FROM dual; -- 输出结果为'14-MAR-22 12.00.00.000000 AM'
CONCAT() function Used to concatenate two or more strings to generate a new string. Its syntax is as follows:
CONCAT(string1, string2 [, string3, ...])
Among them, string1, string2, etc. represent the strings to be connected. The following is an example of using the CONCAT() function:
SELECT CONCAT('Hello',' ','world','!') FROM dual; -- 输出结果为'Hello world!'
Summary
The above introduces several common character conversion functions in Oracle, they are TO_CHAR(), CAST() and CONCAT(). We can choose which function to use to convert data types based on actual needs. It should be noted that when using these functions, we should understand their syntax and usage in order to better complete the tasks we need.
The above is the detailed content of Detailed introduction to several commonly used character conversion functions in Oracle. For more information, please follow other related articles on the PHP Chinese website!