Home >Database >Mysql Tutorial >How to Extract YYYY/MM/DD Dates from PostgreSQL Timestamps?

How to Extract YYYY/MM/DD Dates from PostgreSQL Timestamps?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-29 21:31:18633browse

How to Extract YYYY/MM/DD Dates from PostgreSQL Timestamps?

Extracting Date (YYYY/MM/DD) from PostgreSQL Timestamps

Need to isolate the date component from timestamps in PostgreSQL? Here's how:

Using the ::date Suffix

Simply append ::date to your timestamp to get a DATE type value. For instance, with the timestamp '2010-01-01 12:00:00'::timestamp, the result is:

'2010-01-01 12:00:00'::timestamp::date;
>> 2010-01-01

Employing the date_trunc Function

Unlike the ::date suffix which preserves the time zone, date_trunc returns a date value in the same data type as the timestamp, retaining the time zone if necessary. Consider the example:

select date_trunc('day', now());
>> 2015-12-15 00:00:00+02

The above is the detailed content of How to Extract YYYY/MM/DD Dates from PostgreSQL Timestamps?. 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