Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

SQL Query to Convert Date to Datetime

  • Difficulty Level : Medium
  • Last Updated : 18 Oct, 2021

In this article, we will look at how to convert Date to Datetime. We can convert the Date into Datetime in two ways. 

  • Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type.Convert() function is used to convert a value of any type to another datatype.
  • Using the CAST() function: SQL Server uses the CAST() function to cast or convert a value or an expression from one data type to another. The Cast() function is also used for the same purpose to convert the data type of any value.

To perform any queries we have to create a database. So, let us create a database first.

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

Step 1: Creating Database

Query:



CREATE DATABASE Test;

Output:

Step 2: Converting Date to Datetime

Method 1: Using CONVERT() function

In this example, we are converting the date 01-01-2021 into Datetime. The date is in the form ‘yyyy-mm-dd’.

Query:

SELECT CONVERT(datetime, '2021-01-01');

Output:

Method 2: Using CAST() function

In this example, we are converting the date 01-01-2021 into Datetime as shown below. The date is in the form ‘yyyy-mm-dd’.

Query:

SELECT CAST('2021-01-01' AS datetime);

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :