How is the TOCHAR function used in Oracle?
The TO_CHAR function in Oracle is used to convert numerical, date, and character data types into strings. Its basic syntax is as follows:
Convert the given expression to a character data type using the specified format and NLS parameters.
In this, ‘expr’ represents the value to be converted; ‘format’ represents the format of the converted string; ‘nlsparam’ represents optional internationalization parameters.
Here are some common usage examples:
- Convert date-type data to a specified format string:
- Convert the current date and time to a specified string format using TO_CHAR(SYSDATE, ‘YYYY-MM-DD HH24:MI:SS’).
- Convert numerical data to a specified format of string.
- Convert the numeric value 12345.67 to a string with a thousand separator as “12,345.67” using TO_CHAR(12345.67, ‘9,999.99’).
- Convert character data to a string in the specified format.
- The function TO_CHAR(‘123’, ‘00000’) converts the string “123” into a 5-digit string “00123”.
- Convert using internationalization parameters:
- The TO_CHAR(SYSDATE, ‘Day’, ‘NLS_DATE_LANGUAGE=english’) function converts the current date into a string of the day of the week in English.
The examples above are just some basic usage cases of the TO_CHAR function. The specific usage can be adjusted according to actual needs. For more detailed usage, refer to the Oracle official documentation or related tutorials.