Open In App

SQL Query to Convert Date to Datetime

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

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:

Article Tags :
SQL