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
Release: 2024-12-27 14:02:10
Original
232 people have browsed it

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);
Copy after login

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);
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template