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
CREATE DATABASE Test;
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’.
SELECT CONVERT(datetime, '2021-01-01');
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’.
SELECT CAST('2021-01-01' AS datetime);
Last Updated :18 Oct, 2021