Advanced Date and Time Calculations
ã¡ãã¥ãŒã衚瀺ããã«ã¯ã¹ã¯ã€ãããŠãã ãã
Here is the corrected version adapted specifically for PostgreSQL, without mentioning SQL Serverâonly functions like DATEPART or FORMAT:
When you need to perform advanced date and time calculations in SQL, PostgreSQL provides powerful built-in functions for extracting date parts, formatting timestamps, and handling time zones. These tools allow you to break down dates for analysis, present them in readable formats, and work safely with different time zones.
The EXTRACT function is used to pull specific components from a date or timestampâsuch as the year, month, day, or hour. This is especially useful for grouping or filtering data by time periods:
EXTRACT(MONTH FROM order_date)
For formatting dates into custom patterns (for example, for reports or exports), PostgreSQL uses the TO_CHAR function:
TO_CHAR(order_date, 'YYYY-MM-DD')
PostgreSQL also has strong timezone support. You can convert timestamps between time zones with the AT TIME ZONE operator:
order_timestamp AT TIME ZONE 'UTC'
Together, these features make PostgreSQL well-suited for detailed date processing, reporting, and timezone-aware analytics.
1234567SELECT EXTRACT(MONTH FROM order_date) AS order_month, COUNT(*) AS total_orders FROM orders GROUP BY order_month ORDER BY order_month;
When generating reports, it is often necessary to display dates in a clear, standardized format.
In PostgreSQL, the TO_CHAR function is used to convert dates and timestamps into human-readable strings. This allows you to format dates as âYYYY-MM-DDâ for data exports or âMonth DD, YYYYâ for printed summaries.
123456-- Project future delivery dates by adding 3 days to each order_date SELECT order_id, order_date, (order_date + INTERVAL '3 day') AS projected_delivery_date FROM orders;
1. Which function extracts a specific part (like month) from a date?
2. What is the purpose of DATEADD?
3. Fill in the blanks to format a date as 'YYYY-MM-DD':
ãã£ãŒãããã¯ããããšãããããŸãïŒ
AIã«è³ªåãã
AIã«è³ªåãã
äœã§ã質åããããææ¡ããã質åã®1ã€ã詊ããŠãã£ãããå§ããŠãã ãã