UTC_TIMESTAMP

UTC_TIMESTAMP is a function in SQL that returns the current UTC date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context.

UTC_TIMESTAMP([fractional_seconds_precision])

  • fractional_seconds_precision: This parameter determines the number of digits in the fractional part of the SECOND. The value for the parameter can range between 0 and 6, where the default is 0 when it’s not specified. It specifies the microseconds precision to be used in the generated timestamp.

Example

SELECT UTC_TIMESTAMP();

Output

+---------------------+
| UTC_TIMESTAMP() |
+---------------------+
| 2022-01-01 12:30:30 |
+---------------------+

Explanation

The UTC_TIMESTAMP() function returns the current UTC date and time as a value in ‘YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.

UTC_TIMESTAMP()

Example

SELECT NOW() AT TIME ZONE 'utc';

Output

2022-03-17 07:32:30.78978+00

Explanation

In PostgreSQL, the function NOW() AT TIME ZONE 'utc' is used to get the current timestamp in UTC. It returns the current date and time in UTC.

UTC_TIMESTAMP is not available in SQL Server. Instead, SQL Server uses GETUTCDATE() to return the current database system timestamp as a datetime value. The equivalent syntax in SQL Server is:GETUTCDATE()

Example

SELECT GETUTCDATE() AS UTC_TIMESTAMP;

Output

UTC_TIMESTAMP
-----------------------
2022-07-14 14:07:21.393

Explanation

In SQL Server, the GETUTCDATE() function returns the current UTC date and time as a datetime value. The returned datetime value is in ‘yyyy-mm-dd hh:mi:ss.mmm’ format.

The example demonstrates how to use this function. The current UTC datetime is selected and returned with the column name ‘UTC_TIMESTAMP’.

UTC_TIMESTAMP() RETURNS TIMESTAMP

Oracle SQL does not have a built-in UTC_TIMESTAMP function, but you can use the combination of SYSTIMESTAMP and AT TIME ZONE to get the UTC timestamp. Here are the details.

Example

SELECT SYSTIMESTAMP AT TIME ZONE 'UTC' AS UTC_TIMESTAMP FROM dual;

Output

UTC_TIMESTAMP
-----------------------------
01-AUG-21 10.10.11.123000000 UTC

Explanation

The SYSTIMESTAMP AT TIME ZONE 'UTC' statement retrieves the current system timestamp and converts it into the UTC timezone. The FROM dual is necessary for SELECT statements that do not rely on a table to retrieve information.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.