MySQL DAYNAME Function

Created with Sketch.

MySQL DAYNAME Function

 Summary: in this tutorial, you will learn how to use the MySQL DAYNAME function to get the name of a weekday for a given date.

Introduction to MySQL DAYNAME function

MySQL DAYNAME function returns the name of a weekday for a specified date. The following illustrates the syntax of the DAYNAME function:

DAYNAME(date);

Code language: SQL (Structured Query Language) (sql)

The DAYNAME function accepts 1 argument which is a date that you want to get the name of its weekday.

If the date is NULL or invalid e.g., 2017-02-30, the DAYNAME function returns NULL.

MySQL DAYNAME function examples

The following example returns the name of a weekday for January 1st, 2000.

mysql> SELECT DAYNAME('2000-01-01') dayname;
+----------+
| dayname |
+----------+
| Saturday |
+----------+
1 row in set (0.00 sec)

Code language: SQL (Structured Query Language) (sql)

By default, MySQL returns the name of a weekday in the language controlled by the lc_time_names system variable.

mysql> SELECT @@lc_time_names;
+-----------------+
| @@lc_time_names |
+-----------------+
| en_US |
+-----------------+
1 row in set (0.00 sec)

Code language: SQL (Structured Query Language) (sql)

As you can see, currently, the locale is set to en_US.

To get day name a weekday in a specific locale, you need to change the value of the lc_time_names variable. For example, the following statement sets the locale to French:

mysql> SET @@lc_time_names = 'fr_FR';
Query OK, 0 rows affected (0.00 sec)

Code language: SQL (Structured Query Language) (sql)

Now, let’s query the name of weekday for January 1st, 2000:

mysql> SELECT DAYNAME('2000-01-01') dayname;
+---------+
| dayname |
+---------+
| samedi |
+---------+
1 row in set (0.00 sec)

Code language: SQL (Structured Query Language) (sql)

As you can see, the weekday name has been changed to French.

The following table shows the valid locales for lc_time_names supported by MySQL:

Albanian – Albaniasq_AL
Arabic – Algeriaar_DZ
Arabic – Bahrainar_BH
Arabic – Egyptar_EG
Arabic – Indiaar_IN
Arabic – Iraqar_IQ
Arabic – Jordanar_JO
Arabic – Kuwaitar_KW
Arabic – Lebanonar_LB
Arabic – Libyaar_LY
Arabic – Moroccoar_MA
Arabic – Omanar_OM
Arabic – Qatarar_QA
Arabic – Saudi Arabiaar_SA
Arabic – Sudanar_SD
Arabic – Syriaar_SY
Arabic – Tunisiaar_TN
Arabic – United Arab Emiratesar_AE
Arabic – Yemenar_YE
Basque – Basqueeu_ES
Belarusian – Belarusbe_BY
Bulgarian – Bulgariabg_BG
Catalan – Spainca_ES
Chinese – Chinazh_CN
Chinese – Hong Kongzh_HK
Chinese – Taiwan Province of Chinazh_TW
Croatian – Croatiahr_HR
Czech – Czech Republiccs_CZ
Danish – Denmarkda_DK
Dutch – Belgiumnl_BE
Dutch – The Netherlandsnl_NL
English – Australiaen_AU
English – Canadaen_CA
English – Indiaen_IN
English – New Zealanden_NZ
English – Philippinesen_PH
English – South Africaen_ZA
English – United Kingdomen_GB
English – United Statesen_US
English – Zimbabween_ZW
Estonian – Estoniaet_EE
Faroese – Faroe Islandsfo_FO
Finnish – Finlandfi_FI
French – Belgiumfr_BE
French – Canadafr_CA
French – Francefr_FR
French – Luxembourgfr_LU
French – Switzerlandfr_CH
Galician – Spaingl_ES
German – Austriade_AT
German – Belgiumde_BE
German – Germanyde_DE
German – Luxembourgde_LU
German – Switzerlandde_CH
Greek – Greeceel_GR
Gujarati – Indiagu_IN
Hebrew – Israelhe_IL
Hindi – Indiahi_IN
Hungarian – Hungaryhu_HU
Icelandic – Icelandis_IS
Indonesian – Indonesiaid_ID
Italian – Italyit_IT
Italian – Switzerlandit_CH
Japanese – Japanja_JP
Korean – Republic of Koreako_KR
Latvian – Latvialv_LV
Lithuanian – Lithuanialt_LT
Macedonian – FYROMmk_MK
Malay – Malaysiams_MY
Mongolia – Mongolianmn_MN
Norwegian – Norwayno_NO
Norwegian(Bokmål) – Norwaynb_NO
Polish – Polandpl_PL
Portugese – Brazilpt_BR
Portugese – Portugalpt_PT
Romanian – Romaniaro_RO
Russian – Russiaru_RU
Russian – Ukraineru_UA
Serbian – Yugoslaviasr_RS
Slovak – Slovakiask_SK
Slovenian – Sloveniasl_SI
Spanish – Argentinaes_AR
Spanish – Boliviaes_BO
Spanish – Chilees_CL
Spanish – Columbiaes_CO
Spanish – Costa Ricaes_CR
Spanish – Dominican Republices_DO
Spanish – Ecuadores_EC
Spanish – El Salvadores_SV
Spanish – Guatemalaes_GT
Spanish – Hondurases_HN
Spanish – Mexicoes_MX
Spanish – Nicaraguaes_NI
Spanish – Panamaes_PA
Spanish – Paraguayes_PY
Spanish – Perues_PE
Spanish – Puerto Ricoes_PR
Spanish – Spaines_ES
Spanish – United Stateses_US
Spanish – Uruguayes_UY
Spanish – Venezuelaes_VE
Swedish – Finlandsv_FI
Swedish – Swedensv_SE
Tamil – Indiata_IN
Telugu – Indiate_IN
Thai – Thailandth_TH
Turkish – Turkeytr_TR
Ukrainian – Ukraineuk_UA
Urdu – Pakistanur_PK
Vietnamese – Viet Namvi_VN

See the following orders table in the sample database:

The following statement returns the order count grouped by name of weekday in 2004.

SELECT
DAYNAME(orderdate) weekday,
COUNT(*) total_orders
FROM
orders
WHERE
YEAR(orderdate) = 2004
GROUP BY weekday
ORDER BY total_orders DESC;

Code language: SQL (Structured Query Language) (sql)

The number of orders placed on Friday is the highest and there were two orders placed on Sunday.

In this tutorial, you have learned how to use the MySQL DAYNAME function to get the name of weekday for a particular date.

Leave a Reply

Your email address will not be published. Required fields are marked *