Home >Database >Mysql Tutorial >How to Discard Milliseconds and Seconds from a Timestamp Without Time Zones?

How to Discard Milliseconds and Seconds from a Timestamp Without Time Zones?

DDD
DDDOriginal
2024-12-27 14:02:10244browse

How to Discard Milliseconds and Seconds from a Timestamp Without Time Zones?

Discarding Timestamp Milliseconds and Seconds

How can you eliminate the millisecond and second components from a timestamp without using a timezone?

Discarding Using Cast

Casting a timestamp to timestamp(0) or timestamptz(0) will round the value to the nearest whole second. For example:

SELECT now()::timestamp(0);

Truncating Using date_trunc()

The date_trunc() function can truncate a timestamp, leaving the seconds unchanged. This is often a more desirable option than rounding. For example:

SELECT date_trunc('second', now()::timestamp);

Syntax

date_trunc accepts input for the first argument "field". Use the following values to truncate:

  • minute: Truncate to the minute, discarding seconds.
  • second: Truncate to the second, leaving seconds unchanged.

Output

The data type of the return value will match the input data type (timestamp, timestamptz, or interval).

The above is the detailed content of How to Discard Milliseconds and Seconds from a Timestamp Without Time Zones?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn