DAY() function is used to get day from the date provided in the DAY() function. It gives the single day result in positive integer between first day of the month to last day of the month.
General Syntax is as follows:
Get day from GETDATE() Function
We can get day from GETDATE() function which gives the day in integer from the current date.
SELECT DAY(GETDATE()) AS DayFromGetDate
Get day from date provided as string in single quotes
We can get day from date provided as string in single quotes which gives the day in integer.
SELECT DAY('2022/01/31') AS DayFromDate
Get day from table Column
We can get day from table having DATETIME data type. To get the day from the table columns, first we create a table and insert some rows and then we will get the day from that particular column.
USE HR GO CREATE TABLE tblEmployee ( EmpID INT PRIMARY KEY NOT NULL, EmpName VARCHAR(20), EmpGender VARCHAR(10), EmpAge INT, EmpSalary INT, EmpJoiningDate DATETIME ) GO
Let’s insert some records into tblEmployee table.
USE HR GO INSERT INTO tblEmployee VALUES (1,'Simon','Male',25,25000,'2015-01-01 22:04:49.400'), (2,'Dave','Male',29,15000,'2016-03-25 20:04:55.400'), (3,'Sara','Female',35,20000,'2017-05-01 18:04:35.400'), (4,'Julia','Female',36,35000,'2018-01-10 15:03:45.400'), (5,'Sam','Male',32,35000,'2020-03-20 10:02:25.400') GO
Below is the result of records of table tblEmployee.
Now, below is the query to get day from the EmpJoiningDate column of the table tblEmployee.
USE HR GO SELECT EmpName, EmpGender, EmpSalary, DAY(EmpJoiningDate) AS JoiningDay FROM tblEmployee GO