SQL DAY Function

SQL Day function will return an integer that represents the day of a specified Date. Thes yntax of the DAY Function is

DAY(date)

date: It can be an expression that returns the date, or you can use the date and time directly. For this Day function example, We use this data

SQL DAY Function 1

SQL DAY Function Example

In this example, we will show you the possible ways to use the 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 2

In this Date and Time Function example, we will extract the Date from Employee table using the DATEPART Function, and Day function.

-- 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]

As you can observe that the DATEPART function, and Day function are returning the same result.

SQL DAY Function 3

Remember, before SQLServer 2012, people used the DATEPART function to extract the day. But, in 2012 version, Microsoft introduced the day function.