Introduction to PostgreSQL Date Functions
In PostgreSQL variety of date functions are available that are used to manipulate timestamps. The date function is very useful and important in PostgreSQL; in date functions, inputs come in two formats:
- one is time with time zone or timestamp with time zone and
- another input comes with time without time zone or timestamp without time zone
The most commonly used date function in PostgreSQL are now (), date_part (), age (), extract (), date_trunc (), to_char () and to_timestamp ().
All PostgreSQL Date Functions
Below is the common date functions that are as follows.
- Now ()
- Now ()::date
- date_part ()
- age ()
- extract ()
- date_trunc ()
- current_date ()
- to_timestamp ()
- justify ()
We have using an employee table to describe the date function. Please find below the example to create an employee table.
CREATE TABLE Employee ( emp_id INT NOT NULL, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL, date_of_joining date NOT NULL, PRIMARY KEY (emp_id));
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary, date_of_joining) VALUES (1, 'ABC', 'Pune', '1234567890', 20000, '01-01-2020');
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary, date_of_joining) VALUES (2, 'PQR', 'Pune', '1234567890', 20000, '01-01-2020');
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary, date_of_joining) VALUES (3, 'XYZ', 'Mumbai', '1234567890', 35000, '02-01-2020');
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary, date_of_joining) VALUES (4, 'BBS', 'Mumbai', '1234567890', 45000, '02-01-2020');
1. NOW ()
- To select the current date, we have used the now () function.
- Now the function will return the date and time with the time zone from which the current transaction started.
- The return type of now () function is timestamptz.
- Below are the syntax and example of the now () function.
Syntax
Select now ();
Example
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary, date_of_joining) VALUES (7, 'RBS', 'Delhi', '1234567890', 50000, now());
select now();
select * from Employee where emp_id = 7;
select now() - date_of_joining as no_of_day from employee;
2. Now ()::date
To select the date without timestamp at the same time, we have to use now ():: date function.
Below are the example and syntax of the now ():: date function.
Syntax
Select now ():: date;
Example
select now()::date;
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary, date_of_joining) VALUES (8, 'RBS', 'Delhi', '1234567890', 50000, now()::date);
select now()::date - date_of_joining as no_of_day from employee where emp_id=1;
3. Date_part ()
Return type of date_part () function is double-precision value.
To get the timestamp field or an interval like the year, month, the day that time, we have using the date_part () function.
Below is the example, and the syntax of the date_part () function are as follows.
Syntax
Date_part (subfield (month, day, year) from timestamp)
Example
select date_part('days', now() - date_of_joining) as days from employee limit 3;
4. Age ()
We have found an interval between two dates using the age () function in PostgreSQL.
The return type of age () function is an interval.
It will calculate ages between two timestamps’ current date and timestamps; after calculating, it returns symbolic results.
Below is the example and syntax of the age () function in PostgreSQL.
Syntax
Age (timestamp)
Example
select age(date_of_joining) from employee limit 3;
5. extract ()
The return type of extract function is double-precision value.
Extract () date function is same as date_part () function in PostgreSQL.
This function allows us to isolate the date between date, month and year fields. The date is isolated between different parts by using the extract () function.
Below are the syntax and example of the extract () function in PostgreSQL.
Syntax
Extract(subfield(month, day, year) from timestamp)
Example
select extract (year from date_of_joining) from employee limit 3;
select extract (month from date_of_joining) from employee limit 3;
select extract (day from date_of_joining) from employee limit 3;
6. date_trunc ()
- Return type of date_trunc () function is timestamp. Date_trunc () function timestamp truncated to a specific precision.
- Below is the example, and the syntax of the date_trunc () function are as follows.
- Date_trunc () function is used to truncate in specified precision.
Syntax
Date_trunc (field (month, day, year) from timestamp)
Example
SELECT date_trunc ('year', timestamp '2020-02-15 11:30:45');
SELECT date_trunc ('month', timestamp '2020-02-15 11:30:45');
SELECT date_trunc ('day', timestamp '2020-02-15 11:30:45');
SELECT date_trunc ('hour', timestamp '2020-02-15 11:30:45');
7. current_date ()
- The return type of the current date function is the date. Below is the example of the current_date () function in PostgreSQL are as follows.
Syntax
select current_date;
Example
select current_date;
8. to_timestamp ()
- PostgreSQL will convert string value into proper date format using to_timestamp () date function in PostgreSQL.
- Below are the example and syntax of the to_timestamp date function in PostgreSQL.
Syntax
to_timestamp ()
Example
select to_timestamp('202015FEB', 'YYYYDDMon') as valid_date;
9. Justify ()
Below is the adjust interval of justifying date function in PostgreSQL.
- JUSTIFY_DAYS (interval) – Adjust interval with 30 days’ time period.
- JUSTIFY_HOURS (interval) – Adjust interval with 24 hours’ time period.
- JUSTIFY_INTERVAL(interval) – Adjust interval with days, hours and additional sign adjustments.
Below is the syntax, and examples of justifying intervals are as follows.
Syntax
JUSTIFY_DAYS (interval)
Example
SELECT justify_days (interval '65 days');
JUSTIFY_HOURS (interval)
Example
SELECT justify_hours (interval '55 hours');
JUSTIFY_INTERVAL(interval)
Example
SELECT justify_interval (interval '2 mon -5 hour');
Conclusion
Date functions are very important and useful. In PostgreSQL variety of date functions available but mainly now (), current_date (), date_part (), extract (), age (), justify (), date_trunc (), to_timestamp () and to_char () date functions used.
Recommended Articles
This is a guide to PostgreSQL Date Functions. Here we discuss the All Functions along with the examples and syntax. You may also have a look at the following articles to learn more –