SQL DAY Function

SQL Day function will return an integer representing a specified date’s day, and the syntax is

DAY(date)

The argument 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 Example

In this example, we will show you the possible ways to use the Day method.

SELECT DAY('12-19-05') AS [DayExample 1]

-- Testing with Random DateTime 
SELECT DAY('2015-05-14 12:29:44.513') AS [DayExample 2]

-- Testing with Todays Date
SELECT DAY(GETDATE()) AS [Todays Date]
Simple DAY Example 2

In this DateTime example, we will extract the Date from the Employee table using the DATEPART and DAY functions. Please refer to the list of SQL Server Date functions available on our SQL Tutorial page.

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, the DATEPART and Day functions return the same result. Remember, before Server 2012, people used the DATEPART to extract the day. But, in the 2012 version, Microsoft introduced the day function.

TIP: Please refer to the SQL DATENAME, SQL MONTH, and SQL YEAR functions.

DAY Function 3
Categories SQL