EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Date Functions

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Date Functions

PostgreSQL Date Functions

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:

  1. one is time with time zone or timestamp with time zone and
  2. 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 ().

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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');

PostgreSQL Date Functions output 1

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;

PostgreSQL Date Functions output 2

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;

PostgreSQL Date Functions output 3

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;

PostgreSQL Date Functions output 4

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;

output 5

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;

output 6

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');

output 7

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;

output 8

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;

output 9

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');

output 10

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 –

  1. PostgreSQL Versions
  2. What is PostgreSQL?
  3. PostgreSQL Operators
  4. PostgreSQL Interview Questions
  5. Guide to PostgreSQL Alias

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More