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 MySQL Day function example, We use this data.

Employee Table 1

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.

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 and Day functions return the same result.

SQL DAY Function 3

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

Categories SQL