Open In App

SQL Query to Convert Date to Datetime

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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.

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:


Last Updated : 18 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads