MySQL - DAYNAME() Function



The MYSQL DAYNAME() function is used to retrieve the name of the weekday of the given date. This function returns the string value which can be either of: Sunday, Monday, Tuesday, Wends day, Thursday, Friday, Saturday Or, NULL.

Syntax

Following is the syntax of MySQL DAYNAME() function −

DAYNAME(date);

Parameters

This method accepts the date value from which you need to get the weekday name as a parameter.

Return value

This function returns the name of the weekday of the provided date.

Example

In the following query, we are using the MySQL DAYNAME() function to extract the day of the week from the date '2019-05-25' −

SELECT DAYNAME('2019-05-25') As Result;

Output

The output for the query above is produced as given below −

Result
Saturday

Example

If the day part in the given date is 0 this function returns NULL.

SELECT DAYNAME('2017-00-00') As Result;

Output

This will produce the following result −

Result
NULL

Example

If we pass an empty string or a non-string value as an argument this function returns NULL.

SELECT DAYNAME('') As Result;

This will produce the following result −

Result
NULL

Here, we are passing a non-string value as argument to this function −

SELECT DAYNAME(1990-11-11) As Result;

This will produce the following result −

Result
NULL

Example

In the following example, we are retrieving weekday name from the current date −

SELECT DAYNAME(CURDATE()) As Result;

Output

This output is displayed as follows −

Result
Saturday

Example

We can also pass the column name as an argument to this function. Let us create a table with name ORDERS using CREATE statement as shown below −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

Now, insert the following records into the ORDERS table using the INSERT statement −

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From ORDERS;

Following is the ORDERS table −

OID DATE CUSTOMER_ID AMOUNT
102 2009-10-08 00:00:00 3 3000.00
100 2009-10-08 00:00:0 3 1500.00
101 2009-11-20 00:00:00 2 1560.00
103 2008-05-20 00:00:00 4 2060.00

Now, we are using the MySQL DAYNAME() function to retrieve the name of the day from the DATE column −

SELECT OID, DATE, DAYNAME(DATE) From ORDERS;

This will produce the following result −

OID DATE DAYNAME(DATE)
102 2009-10-08 00:00:00 Thursday
100 2009-10-08 00:00:0 Thursday
101 2009-11-20 00:00:00 Friday
103 2008-05-20 00:00:00 Tuesday
Advertisements