CAST to Date Data Type
The CAST method directly converts the datetime value to date data type which removes the time component.
Syntax:
CAST(DateTimeValue AS Date)
Explanation: For demonstrating the CAST function using a Select Query, we will use a table with a datetime column and sample data as below:
Example 1: Fetch Employee Details and DateJoined C
olumn
Let’s Retrieve and display the employee ID, employee name, and cast the DateJoined
column as a Date type in the ‘Date_Of_Join‘ column from the table EmployeesInfo
in a MariaDB database.
Query:
Select EmployeeID,EmployeeName,CAST(DateJoined AS Date) as Date_Of_Join from EmployeesInfo
Output:
Explanation: In the above Query we have fetched the mentioned data.
How to Remove Times from Dates in SQL Server
In SQL Server, there are Date and DateTime data types to store Date and Time values. There can be situations when only the date value needs to be displayed or to do date calculations for date add, date difference functions, and between two dates from datetime values. So, to remove the Time part from dates with data and time, many methods can be used based on need and usage.
In this article, we will discuss methods such as CAST to Date Data Type, CONVERT Function with Style Code, and Format Function (For SQL Version 2012 and Later) to Remove Times from Dates in SQL Server in detail.