I thought it would be interesting to run a few quick queries to see how various formatting strings affect the formatting of date/time values.
Tag: date functions
PostgreSQL Date & Time Functions
Below is a full list of date/time functions available in PostgreSQL.
Continue readingCalculate the Age in Years in PostgreSQL
Postgres has the age()
function that returns the age in years, months, and days based on two dates.
This works fine unless you only want to return the age in years.
For example, you simply want to return a person’s age based on their birthday. You want something like 32 instead of 32 years 4 mons 67 days, which is what age()
is likely to return.
Fortunately there’s an easy way to do this in PostgreSQL.
Continue readingGet the Week Number from a Date in PostgreSQL
In PostgreSQL you can use the extract()
function to get the week number from a date.
You can also use the date_part()
function to do the same thing.
Get the Day from a Date in PostgreSQL
In PostgreSQL you can use the extract()
function to get the day from a date.
You can also use date_part()
to do the same thing.
When extracting the day from a date, you need to specify what sense of the word “day” you mean. For example, “day of week”, “day of month”, “day of year”, etc.
How AT TIME ZONE Works in PostgreSQL
In PostgreSQL, you can use the AT TIME ZONE
clause to convert a timestamp to another timezone.
How make_interval() Works in PostgreSQL
In PostgreSQL, the make_interval()
function creates an interval from years, months, weeks, days, hours, minutes and seconds fields.
You provide the years, months, weeks, days, hours, minutes and/or seconds fields, and it will return an interval in the interval data type.
Continue readingHow make_timestamptz() Works in PostgreSQL
In PostgreSQL, the make_timestamptz()
function allows you to create a timestamp with time zone from its year, month, day, hour, minute and seconds fields, as well as an optional timezone field.
How make_date() Works in PostgreSQL
In PostgreSQL, the make_date()
function allows you to create a date from its separate year, month and day fields.
Each date part is provided as an integer, and the result is returned as a date.
Continue readingHow make_time() Works in PostgreSQL
In PostgreSQL, the make_time()
function allows you to create a time from its hour, minute and seconds fields.