PostgreSQL can hold time and date information in the following formats:
TIME→ only time infoDATE→ only date infoTIMESTAMP→ date and timeTIMESTAMPTZ→ date, time, and timezone.
In addition to the types above, there are some functions and operations that are useful when using SQL:
TIMEZONENOWTIMEOFDAYCURRENT_TIMECURRENT_DATE
An important set of operations includes extracting time based data from a table. This is often done using:
EXTRACT()- Used to “extract” or obtain a sub-component of a date value, including:
- YEAR
- MONTH
- DAY
- WEEK
- QUARTER
- Usage:
- Used to “extract” or obtain a sub-component of a date value, including:
EXTRACT(YEAR FROM date_col)1AGE()- Calculates and returns the current age given a timestamp
- Usage:
AGE(date_col) # x years y mon z days timedataTO_CHAR()- General function to convert data types to text
TO_CHAR(date_col, 'mm-dd-yyyy)