Format Function (For SQL Version 2012 and Later)
The FORMAT function can also be used to get only the date part from a datetime expression or date column in a table.
Syntax:
FORMAT(datevalue, format [, culture])
Explanation: In Format function, datevalue is the data expression or date column and format is required date format. Culture is an optional option which is country specific date format.
Example 1: Fetch Employees Details and Formatted Date of Joining
Let’s Retrieve and display the employee ID, employee name, and formatted date of joining from the table EmployeesInfo
in a MariaDB database. The formatted date should be presented in the ‘dd/MM/yyyy’ format.
Query:
Select EmployeeID,EmployeeName,FORMAT( DateJoined, 'dd/MM/yyyy') as Date_Of_Join from EmployeesInfo
Output:
Explanation: We have successfully fetched all information with this format ‘dd/MM/yyyy’.
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.