DAY

DAY is a built-in function in SQL that returns the day of the month from a specified date, as an integer between 1 and 31.

DAY(date)

  • date: The input parameter which specifies the date from which the day will be extracted. It should be in a format that MySQL can recognize, such as ‘YYYY-MM-DD’ or ‘YY-MM-DD’.

Example

SELECT DAY('2021-09-12');

Output

12

Explanation

In the above query, the DAY() function is used. It extracts the day from the date ‘2021-09-12’. The output of this query is ‘12’, because ‘12’ is the day part of the date ‘2021-09-12’.

DAY(interval)

  • interval: It refers to the interval of time from which the day is to be extracted. This could be in the format of ‘Year-Month-Day-Hour’, ‘Month-Day’ or just ‘Day’. DAY(interval) will return the day value from the specified time interval.

Example

SELECT EXTRACT(DAY FROM TIMESTAMP '2021-08-21 00:00:00');

Output

21

Explanation

The EXTRACT(DAY FROM TIMESTAMP) function in Postgres SQL returns the day portion of a particular timestamp. In this example, given the timestamp ‘2021-08-21 00:00:00’, the function will return ‘21’, which is the day portion of the date.

DAY( date )

  • date: The input date from which the ‘day’ part is to be extracted. The input ‘date’ should be in a format that’s compatible with any SQL Server’s date and time types (DATE, DATETIME, DATETIME2, DATETIMEOFFSET, SMALLDATETIME, and TIMESTAMP).

Example

SELECT DAY('2021-12-31') AS DayOfMonth;

Output

DayOfMonth
31

Explanation

The DAY function in SQL Server extracts the day of the month from the date ‘2021-12-31’, which results in ‘31’.

DAY(date)

  • date: This is the date from which the day needs to be extracted. It should be in a format or data type that is convertible to a datetime value. The function will return the day as an integer, ranging from 1 to 31.

Example

SELECT TO_CHAR(TO_DATE('2021-01-01', 'YYYY-MM-DD'),'DAY') AS "Day" from dual;

Output

FRIDAY

Explanation

The TO_CHAR function is utilized in this example to convert a date to a string. Specifically, the DAY format model is used, returning the full name of the weekday. The query retrieves the weekday name for the date ‘2021-01-01’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.