SQL Day function is one of the Date and Time Function, which will return an integer that represent the day of a specified Date. In this article we will show you, How to use the SQL Server Day function with practical example. For this example, We are going to use the below shown data
SQL DAY Syntax
The basic syntax of the DAY Function in SQL Server is as follows:
date: It can be an expression that returns the date, or you can use the date and time directly.
SQL DAY Function Example 1
In this example, we will show you the possible ways to use the SQL Server Day function
-- Example for SQL DAY Function -- Testing Day Function with Random Date SELECT DAY('12-19-05') AS [Day Example 1] -- Testing Day Function with Random Date and Time SELECT DAY('2015-05-14 12:29:44.513') AS [Day Example 2] -- Testing Day Function with Todays Date SELECT DAY(GETDATE()) AS [Todays Date]
SQL DAY Function Example 2
In this example, we are going to extract the Date from Employee table using the DATEPART Function, and Day function in SQL.
-- Example for SQL DAY Function USE [SQL Tutorial] GO SELECT [EmpID] ,[FirstName] + ' '+ [LastName] AS [Full Name] ,[Occupation] ,[YearlyIncome] ,[HireDate] ,DAY([HireDate]) AS [Today] ,DATEPART (day, [HireDate]) AS [TodayFromDatePart] FROM [Employee]
From the above screenshot you can observe that, the DATEPART function, and Day function are returning the same result. Remember, before 2012 people used the DATEPART function to extract the day but in SQL Server 2012, Microsoft introduced the day function.
Thank You for Visiting Our Blog