当前位置: 首页 > news >正文

PostgreSQL - How to convert timestamp to date?

In PostgreSQL, you can convert a timestamp to a date (i.e., drop hours/minutes/seconds) in several common ways:

✅ 1. Cast to date

Fastest and simplest:

SELECT your_timestamp_column::date;

✅ 2. Use CAST()

SELECT CAST(your_timestamp_column AS date);

✅ 3. Use DATE() function

SELECT DATE(your_timestamp_column);

✅ 4. Use date_trunc() (keeps timestamp type)

If you want to keep the timestamp type but zero out time:

SELECT date_trunc('day', your_timestamp_column);

Result: 2025-11-26 00:00:00


Summary

MethodOutput TypeExample Result
timestamp::date date 2025-11-26
CAST(timestamp AS date) date 2025-11-26
DATE(timestamp) date 2025-11-26
date_trunc('day', timestamp) timestamp 2025-11-26 00:00:00