cl.loan_date AS raw_date,
cl.loan_date::DATE AS simple_date,
cl.loan_date::TIMESTAMP AS date_with_time,
cl.loan_date::TIMESTAMPTZ AS date_with_timezone
You can do the same thing when extracting a date from a data array:
json_extract_path_text (ffa.data,'metadata','createdDate') as raw_date (will be TEXT data type)
json_extract_path_text (ffa.data,'metadata','createdDate')::DATE as simple_date,
json_extract_path_text (ffa.data,'metadata','createdDate')::TIMESTAMP as date_with_time,
json_extract_path_text (ffa.data,'metadata','createdDate')::TIMESTAMPTZ as date_with_timezone
TO_CHAR (cl.loan_date::DATE,'mm/dd/yyyy') AS simple_date,
-- When you want the month number (1-12) or weekday number (1-7), use the "Extract" function OR the "date_part" function
-- When you want the month name or weekday name, use the "to_char" function.
-- When you want the numerical day of the month (1-31), use the "date_part" function
date_part ('month', li.loan_date) as month_number_using_date_part
date_part ('day', li.loan_date) as day_of_month_number_using_date_part
date_part ('dow', li.loan_date) as day_of_week_number_using_date_part
to_char (li.loan_date, 'Mon') as month_name_using_tochar
to_char (li.loan_date, 'Dy') as day_name_using_tochar
extract (month FROM li.loan_date) as month_number_using_extract
extract (day FROM li.loan_date) as day_of_month_number_using_extract
extract (dow FROM li.loan_date) as day_of_week_number_using_extract
You can use the "AGE" function or date subtraction to find the time elapsed between two dates. In these example, we are using "CURRENT_DATE" as one of the dates, but you can use any two date fields, cast as the same data type.
AGE (CURRENT_DATE, table.field_name) AS time_elapsed
CURRENT_DATE::DATE - table.field_name::DATE AS days_elapsed
To find the number days elapsed since a given calendar date, use the subtraction function:
current_date -'2022-07-01' as number_of_days_since_7_1_22
To find a calendar date that is some time interval from another date, use the "INTERVAL" function in combination with subtraction (or addition, as needed):
to_char ((current_date - interval '33 years 9 months 26 days')::date,'mm/dd/yyyy') AS date_record_created
...result displays as a date: 03/19/1989 – because that's how we formatted the expression with TO_CHAR
age (current_date,'1989-03-17') AS amount_of_time_in_collection
...result displays as a time interval: 33 years 9 mons 26 days