EDUCBA

EDUCBA

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

PostgreSQL Compare Date

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

PostgreSQL Compare Date

Definition of PostgreSQL Compare Date

PostgreSQL compare date is used to compare date between two different dates which we have used as an input. We can compare the date by using where and between clauses, we can also compare the date using date_trunc function in PostgreSQL. We can also compare the date with timestamp by using the where clause, where clause is very important while comparing date in PostgreSQL.We have to compare date using select and update query using two different dates, after comparing the result it will display the result using select query and update query it will update the rows.

How to Compare Date in PostgreSQL?

  • The below example shows how to compare the date.
  • Where and between clause is useful when we have to compare date in PostgreSQL.
  • We have using the stud_cmp table to describe the example of compare date in PostgreSQL is as follows.
  • Below is the table and data description of the stud_cmp table.

select * from stud_cmp;
\d+ stud_cmp;

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

PostgreSQL Compare Date-1.1

1. Compare Date by Using Where Clause

  • We can compare date by using where clause. The below example shows that compare the two date columns by using the where clause.
Using Select Operations
  • In the below example, we have using select operation on the stud_cmp table to retrieve data by comparing two dates.
  • We have to compare the start_date and end_date column to compare the two date.
  • We have used the AND clause with where clause to compare the two dates in PostgreSQL are as follows.

select * from stud_cmp where start_date = '2020-01-01' and end_date = '2020-01-02';

PostgreSQL Compare Date-1.2

  • In the above example after comparing the start date and end date result will display the three records which contains the comparison between the ‘2020-01-01’ and ‘2020-01-02’;
  • In the Below example, we have compared the date interval and date functions.

SELECT * FROM stud_cmp WHERE start_date >= '2020-01-01'::date AND end_date < ('2020-02-01'::date + '1 day'::interval);

PostgreSQL Compare Date-1.3

Using Update Operations
  • We have compared the date in update operations. In the below example, we have compared the date in update operations.
  • We have to compare the start_date and end_date column to update the id of 11 for the date between ‘2020-01-01’ and ‘2020-01-02’;
  • We have used start_date and end_date column for compare date and update the id between those dates.
  • We have used AND clause with where clause to update the ID of those dates which was compared using two columns.

update stud_cmp set id = 11 where start_date = '2020-01-01' and end_date = '2020-01-02';
select * from stud_cmp where id = 11;

PostgreSQL Compare Date-1.4

2. Compare Date by Using Between Clause

  • We can compare the date in PostgreSQL by using between clause. The below example shows that compare the two date columns by using the between clause.
  • Using between clause we can compare the date of a single column. We have used a single column to compare the date using a clause in PostgreSQL.
Using Select Operations
  • In the below example, we have using select operation on the stud_cmp table to retrieve data by comparing two dates using the between clause.
  • We have to compare the start_date column to compare the data and retrieve the result.
  • We have usedAND clause with between and where clause to compare the two date in PostgreSQL is as follows.

select * from stud_cmp where start_date between '2020-01-01' and '2020-01-02';

PostgreSQL Compare Date-3.1

  • In the above example after comparing start date will display the six records which contains the comparison between the ‘2020-01-01’ and ‘2020-01-02’;
  • In the Below example, we have compared the date interval and date functions by using between clause.

SELECT * FROM stud_cmp WHERE start_date between '2020-01-01'::date AND ('2020-02-01'::date + '1 day'::interval);

Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (2,938 ratings)
Course Price

View Course

Related Courses

PostgreSQL Compare Date-3.2

Using Update Operations
  • We have compared the date in update operations by using between clause. In the below example, we have compared the date in update operations by using between clause.
  • We have to compare the start_date column to update the id of 12 for the date between ‘2020-01-01’ and ‘2020-01-02’;
  • We have used AND clause with between and where clause to update the ID of those dates which was compared using two columns.

update stud_cmp set id = 12 where start_date between '2020-01-01' and '2020-01-02';
select * from stud_cmp;

Output-3.3

3. Compare the date by using date_trunc function

  • We can compare the date by using the date_trunc function in PostgreSQL.
Using Select Operations
  • In the below example, we have using select operation on the stud_cmp table to retrieve data by comparing two dates using the date_trunc function.
  • We have used the date_trunc function with where clause to compare the date in PostgreSQL is as follows.

SELECT * FROM stud_cmp WHERE DATE_TRUNC('day', start_date) = '2020-01-01'::timestamp;

Output-4.1

  • In the above example after comparing the start date and with date_trunc functions it will display the three records which contains the comparison between the ‘2020-01-01’ and timestamp;
Using Update Operations
  • We have compared the date in update operations by using date_trunc functions. In the below example, we have compared the date in update operations by using date_trunc function.
  • We have used where clause with date_trunc function to update the ID of those dates which was compared using date_trunc functions.

update stud_cmp set id = 13 WHERE DATE_TRUNC('day', start_date) = '2020-01-01'::timestamp;
select * from stud_cmp;

Output-4.2

Recommended Articles

This is a guide to PostgreSQL Compare Date. Here we also discuss the definition and how to compare date in postgresql along with examples and its code implementation. You may also have a look at the following articles to learn more –

  1. PostgreSQL REINDEX
  2. Array in PostgreSQL
  3. PostgreSQL ARRAY_AGG()
  4. PostgreSQL CTE

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

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
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*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
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More