EDUCBA

EDUCBA

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

SQL TO_DATE()

By Priya PedamkarPriya Pedamkar

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

SQL TO_DATE()

Introduction to SQL TO_DATE()

TO_DATE() function in most SQL database management servers such as PostgreSQL and ORACLE is used to convert data values of character data types such as VARCHAR, NVARCHAR, CHAR etc. to standard DATE data type. The function takes two arguments, first the value of the character data type that has to be converted and second the datetime format in which the first argument has been written.

Some SQL databases such as SQL Server and MYSQL use a different function to convert character values into DATE data type. In SQL server and MYSQL, we can use CONVERT(datetime, ‘date in character type’) and STR_TO_DATE() functions respectively.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax and Parameters

The basic syntax for using the above mentioned date conversion function is as follows :

to_date(text, datetime format);

The syntax for CONVERT() function in SQL server is as follows :

CONVERT(datetime, text);

The syntax for STR_TO_DATE() function in MYSQL is as follows :

STR_TO_DATE(text, datetime format);

Parameters:

  • Text: Data value in character data types like char, text, varchar, nchar, varchar, etc. that has to be converted into date time format.
  • Datetime format: The specific format based on date specifiers in which the mentioned text is written.

Examples of SQL TO_DATE()

Following are the examples are given below:

Example #1

Basic SQL queries to illustrate the working of to_date() function in PostgreSQL and Oracle SQL databases.

Popular Course in this category
MS SQL Training (13 Courses, 11+ Projects)13 Online Courses | 11 Hands-on Projects | 62+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,981 ratings)
Course Price

View Course

Related Courses
JDBC Training (6 Courses, 7+ Projects)PHP Training (5 Courses, 3 Project)Windows 10 Training (4 Courses, 4+ Projects)SQL Training Program (7 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)Oracle Training (14 Courses, 8+ Projects)

Suppose you want to convert ‘20200526’ into YYYY-MM-DD format (stands for 4 characters of the year, followed by two characters of month and day each.) We can use the to_date() function in the following manner.

SELECT to_date('20200526','YYYYMMDD');

SQL TO_DATE()-1.1

Next, suppose we have some date information written in text format like the one in this example. We can use the following piece of code to perform the task.

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

SQL TO_DATE()-1.2

Suppose we have some entries which are in shorthand format like ‘070920’ and we want to convert it into YYYY-MM-DD format. The following query can help us.

SELECT TO_DATE('070920', 'MMDDYY');

Output-1.3

We can sometimes have date information mixed with a timestamp string. This can be solved by using the to_date function with the following set of arguments.

SELECT TO_DATE('2020-05-26 13:27:18', 'YYYY-MM-DD HH24:MI:SS');

Output-1.4

In PostgreSQL, we can simply convert a character string written in date format to date data type without using the to_date() function in the following way.

SELECT '2020/02/03'::date;

Output-1.5

Great, we just learned to convert character strings into date data type using to_date() function. Since the SQL server and MYSQL do not have to_date() as a built-in function, we cannot use it there. So, let us try some examples which will work there as well.

SELECT STR_TO_DATE('26,5,2020','%d,%m,%Y');

The above mentioned query returns the input string in YYYY-MM-DD date format in the MYSQL database.

Output-1.6

SELECT CONVERT(DATETIME, '2020-05-26');

SQL TO_DATE()-1.7

Example #2

SQL query to illustrate the use of to_date() function in INSERT statements.

In order to illustrate the uses of to_date() function, let us create a dummy “students” table. We can use the following code snippet to perform the given task.

CREATE TABLE students (
student_id INT GENERATED BY DEFAULT AS IDENTITY,
first_name VARCHAR ( 255 ) NOT NULL,
last_name VARCHAR ( 255 ) NOT NULL,
birth_date DATE NOT NULL,
PRIMARY KEY ( student_id )
);

Now let us try to insert data records in the “students” table, using the following queries.

INSERT INTO students(first_name, last_name, birth_date)
VALUES('Kritika','Sharma', TO_DATE('May 01 2000','Mon DD YYYY'));
INSERT INTO students(first_name, last_name, birth_date)
VALUES('Rohit','Verma', TO_DATE('070798','MMDDYY'));
INSERT INTO students(first_name, last_name, birth_date)
VALUES('Ariel','Winter', TO_DATE('2001-02-20 13:27:18', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO students(first_name, last_name, birth_date)
VALUES('Mathew','Jones', TO_DATE('2020-52-5', 'IYYY-IW-ID'));

In these queries, the given birth_date is not in the standard PostgreSQL date format. The first three birth_dates are in familiar formats as we have already discussed examples based on them. The fourth query has birth_date written in Year, week number, and weekday format. We have to use to_date() to convert it into standard YYYY-MM-DD format as shown in the code snippet given above.

Now, let us check if the birth_dates for the given students have been inserted in the required format. We can use the following SELECT statement to fetch records from the students table.

SELECT student_id,
first_name,
last_name,
birth_date
FROM students;

SQL TO_DATE()-2.1

We can clearly observe that the birth_dates have been successfully converted to standard date format (YYYY-MM-DD) using to_date() function.

Recommended Articles

This is a guide to SQL TO_DATE(). Here we also discuss the introduction and syntax and parameters along with different examples and its code implementation. you may also have a look at the following articles to learn more –

  1. MySQL Administration
  2. SQL Bulk Insert
  3. PostgreSQL Like
  4. SQL REGEXP

MS SQL Training (13 Courses, 11+ Projects)

13 Online Courses

11 Hands-on Projects

62+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • SQL Window Functions
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • ANY in SQL
    • LIKE Query in SQL
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL MOD()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Server Permission
    • T-SQL INSERT
    • SQL Ranking Function
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
    • SQL Super Key
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • SQL Formatter
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL INSTR()
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

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 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
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
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 - MS SQL Training (13 Courses, 11+ Projects) Learn More