Home > Database > Mysql Tutorial > How to Extract the Date from a Timestamp in PostgreSQL?

How to Extract the Date from a Timestamp in PostgreSQL?

DDD
Release: 2024-12-31 10:45:11
Original
194 people have browsed it

How to Extract the Date from a Timestamp in PostgreSQL?

Extracting Date from Timestamp in PostgreSQL

When working with timestamps in PostgreSQL, there may be instances when you need to extract only the date component. Here are the optimal approaches to achieve this:

Using ::date Cast

The simplest method is to append the ::date cast to your timestamp. This converts it directly to a DATE data type, removing the time component:

SELECT timestamp_field::date FROM table_name;
Copy after login

Example

SELECT '2011/05/26 09:00:00'::date;

Output:
2011/05/26
Copy after login

Using date_trunc Function (With Time Zone Information)

Alternatively, you can employ the date_trunc function. While it retains the timestamp's data type, the time zone information is preserved:

SELECT date_trunc('day', timestamp_field) FROM table_name;
Copy after login

Example

SELECT date_trunc('day', '2010-01-01 12:00:00'::timestamp);

Output:
2010-01-01 12:00:00+02
Copy after login

The above is the detailed content of How to Extract the Date from a Timestamp in PostgreSQL?. 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