How is the function “totimestamp” used in Oracle?
In Oracle database, the TOTIMESTAMP function is used to convert a string into a TIMESTAMP data type.
Its syntax is as follows:
Converts a string to a timestamp with the specified format and language parameters.
Explanation of parameters:
- string: the string to be converted to a TIMESTAMP.
- Format: Optional parameter specifying the format of the input string. If this parameter is not provided, Oracle will parse the input string based on the current setting of the NLS_TIMESTAMP_FORMAT parameter.
- nlsparam: Optional parameter specifying the parameters of the NLS language environment. If not provided, Oracle will use the settings of the NLS language environment for the current session.
Example usage:
- Retrieve the timestamp in the default format from ‘2022-01-01 12:00:00’ and display it as ‘2022-01-01 12:00:00’.
- Specified format:
SELECT TO_TIMESTAMP(‘2022/01/01 12:00:00’, ‘YYYY/MM/DD HH24:MI:SS’) FROM DUAL;
– Output: 2022-01-01 12:00:00 - Using NLS parameters:
SELECT TOTIMESTAMP(’01-Jan-2022 12:00:00′, ‘DD-Mon-YYYY HH:MI:SS’, ‘NLS_DATE_LANGUAGE=ENGLISH’) FROM DUAL;
– Output: 2022-01-01 12:00:00
Please pay attention to the following instructions:
- The input string must adhere to the specified format, otherwise an error will be thrown.
- The TOTIMESTAMP function can handle dates ranging from January 1, 4712 AD to December 31, 9999 AD.
- The result returned by the TOTIMESTAMP function is a data type of TIMESTAMP.