EDUCBA

EDUCBA

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

PostgreSQL TO_DATE()

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL TO_DATE()

PostgreSQL TO_DATE()

Introduction to PostgreSQL TO_DATE()

PostgreSQL TO_DATE() function is used to convert string literal into date value; basically, to_date function will convert the string into a date. We have given two inputs parameter arguments with the to_date function in PostgreSQL, i.e. text and format; the to_date function is very useful and important in PostgreSQL to convert the string into a date value. To_date function returns the date from function as per we have provided input parameter from the to_date function in PostgreSQL. To_date function is used to handle the input formats which the simple casting cannot convert; this function will interpret input by minimal error checking.

Syntax

Below is the syntax of the to_date function in PostgreSQL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • to_date (text, format)OR
  • to_date (text, text)OR
  • to_date (string, format)

Below is the parameter description syntax of the to_date function in PostgreSQL.

  • to_date –To_date function is used to convert a date represented in a character string to a date data type.
  • Text –Text is the first parameter that was accepted by the to_date function in PostgreSQL. Text is nothing but string argument, which we have converted into the date.
  • Format –The second argument is nothing but the input format of the to_date function, which returned the date value. It will define the format of the input string in the form of date parts.
  • String –String argument is defined as which string that we have converted into the date. We have also defined a string as text in the to_date function.

Working of PostgreSQL TO_DATE() function

  • Below is the working of the to_date function in PostgreSQL.
  • The main use of the to_date function in PostgreSQL is to convert string lateral into the date; the to_date function is used to convert the string into date format.
  • We have pass two arguments with the to_date function in PostgreSQL, i.e. string and format.
  • We can format the date value using the to_date function in PostgreSQL are as follows.

Patterns for formatting date values using to_date function in PostgreSQL.

  1. Y,YYY – This is defined as a year in four digits separated by a comma. The comma is separated after the first digits.
  2. YYYY – This format will define as the year in four digits. We have not used any string to divide the year.
  3. YYY – This format will define the last three digits of the year.
  4. YY – This format will define the last two digits of the year.
  5. Y – This format will define the last one digits of the year.
  6. IYYY – This is defined as ISO numbering of year. It will display the last four or more digits as output.
  7. IYY –This format of ISO numbering will define the last three digits of the week numbering year.
  8. IY – This format of ISO numbering will define the last two digits of the week numbering year.
  9. I – It will define the last digit of ISO 8601 week numbering year.
  10. MONTH –It is used to define the English month name in uppercase.
  11. Month – This is defined as a full capitalized English month name.
  12. month – This is defined full lowercase English month name.
  13. MON –This is abbreviated uppercase English month name, i.e. JAN, FEB.
  14. Mon –This is abbreviated capitalized English month name, i.e. Jan, Feb.
  15. mon – This is abbreviated lowercase English month name, i.e. Jan, feb.
  16. MM – This is defined as the month number from 0 to 12.
  17. DAY – This is defined as a full uppercase day name.
  18. Day – This is defined as a full capitalized day name.
  19. day – This is defined as the full lowercase day name.
  20. DY – This is an abbreviated uppercase day name.
  21. Dy – This is an abbreviated capitalized day name.
  22. dy – This is an abbreviated lowercase day name.
  23. DDD – This is defined as the day of the year from (000 to 366).
  24. DD – This is defined as the day of the month from 01 to 31.
  25. D –This is defined as the day of the week, Sunday as 1 and Saturday as 7.
  • We can convert the date using the to_date function as we specified a format; we can use any format as above in the to_date function.
  • If suppose we have to convert the string as 2020 May 20 into date value, we can use the pattern as “DD Mon YYY” or “YYYY Mon dd”.

Examples

Below is an example of the to_date function in PostgreSQL.

Example #1 – Convert the string using the ‘YYYY MM DD’ format

We have converted the ‘2020 05 20’ date using the ‘YYYY MM DD’ format in the below example.

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

SELECT to_date('2020 05 20', 'YYYY MM DD');

Output:

PostgreSQL TO_DATE() output 1

Example #2 – Convert string using ‘DD MON YYYY’ format

We have converted the ’15 MAY 2020′ date using the ‘DD MON YYYY’ format in the below example.

SELECT to_date('15 MAY 2020', 'DD MON YYYY');

Output:

PostgreSQL TO_DATE() output 2

Example #3 – Convert the string using ‘YYYY Mon DD’ format

In the below example, we have converted the ‘2020 May 20′ date using’YYYY Mon DD’ format.

SELECT to_date('2020 May 20','YYYY Mon DD');

Output:

PostgreSQL TO_DATE() output 3

Example #4 – PostgreSQL to_date gotchas

In the below example, we have passing an invalid date string to the to_date function. After passing invalid date sting then we have converted the string into the date format.

SELECT to_date('2020/05/20', 'YYYY/MM/DD');

Output:

output 4

Example #5 – Convert the string using’MMDDYY’ format

We have converted the ‘052020’ date using the ‘MMDDYY’ format in the below example.

SELECT to_date('052020', 'MMDDYY');

Output:

output 5

Recommended Articles

This is a guide to PostgreSQL TO_DATE(). Here we discuss the Working of the PostgreSQL TO_DATE() function and Examples along with functions and outputs. You may also look at the following articles to learn more –

  1. SQL While Loop
  2. PostgreSQL replace
  3. PostgreSQL For Loop
  4. PostgreSQL log

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
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 - PostgreSQL Course (2 Courses, 1 Project) Learn More