📜  oracle to_timestamp - SQL (1)

📅  最后修改于: 2023-12-03 15:33:19.302000             🧑  作者: Mango

Oracle TO_TIMESTAMP - SQL

Oracle TO_TIMESTAMP function is used to convert the character string to a timestamp value. It can be used to convert date and time values in different formats to a standard format.

Syntax

The syntax for TO_TIMESTAMP function in Oracle is:

TO_TIMESTAMP(char, format)

Here, char is the character string to be converted, and format is the format mask that specifies the format of the character string.

Examples

Let's look at some examples to understand the usage of TO_TIMESTAMP function.

Example 1: Convert a string to timestamp

Suppose you have a string '2022-05-25 14:30:00', and you want to convert it to a timestamp value. You can use the following query:

SELECT TO_TIMESTAMP('2022-05-25 14:30:00', 'YYYY-MM-DD HH24:MI:SS') AS timestamp_value FROM dual;

This will return the following output:

TIMESTAMP_VALUE
--------------------
25-MAY-22 02.30.00.000000000 PM
Example 2: Convert a string to timestamp with timezone

Suppose you have a string '2022-05-25 14:30:00 -05:00', and you want to convert it to a timestamp value with timezone. You can use the following query:

SELECT TO_TIMESTAMP('2022-05-25 14:30:00 -05:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM') AS timestamp_value FROM dual;

This will return the following output:

TIMESTAMP_VALUE
--------------------------------
25-MAY-22 02.30.00.000000000 PM
Example 3: Convert a string to timestamp with fractional seconds

Suppose you have a string '2022-05-25 14:30:00.123456', and you want to convert it to a timestamp value with fractional seconds. You can use the following query:

SELECT TO_TIMESTAMP('2022-05-25 14:30:00.123456', 'YYYY-MM-DD HH24:MI:SS.FF6') AS timestamp_value FROM dual;

This will return the following output:

TIMESTAMP_VALUE
-----------------------------
25-MAY-22 02.30.00.123456000 PM
Conclusion

Oracle TO_TIMESTAMP function is a powerful function to convert character strings to timestamp values. It provides a flexible way to handle date and time values in different formats. With the help of format masks, you can easily convert a wide range of date and time formats to a standard format.