Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql WEEKDAY() vs Mysql DayofWeek()

Tags:

function

mysql

I am trying to understand the both but I am getting really confused. Online it says:

MySQL WEEKDAY() returns the index of the day in a week for a given date (0 for Monday, 1 for Tuesday and ......6 for Sunday). MySQL DAYOFWEEK() returns the week day number (1 for Sunday,2 for Monday …… 7 for Saturday ) for a date specified as argument.

Can anyone explain with an example for both? Thank you.

like image 390
Acerace.py Avatar asked Dec 01 '17 08:12

Acerace.py


People also ask

What is SQL weekday?

Definition and Usage The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.

What is the purpose of Dayname () function?

DAYNAME() function : This function in MySQL is used to return the weekday name for a specified date.

How do I get the day of the week from a TIMESTAMP in SQL?

SQL Server has a couple of inbuilt functions to get the day of week from the given date. To get the name of the day of week, you can use DATENAME function and to get the number of the day of week, you can use DATEPART function.

What is time format in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.


1 Answers

A relevant difference is that weekday counts the days of the week from Monday, as follows and start from 0

0=Monday, 1=Tuesday, 2=Wednesday, 3=Thursday, 4=Friday, 5=Saturday, 6=Sunday

On the other hand dayofweek counts the days of the week from Sunday and start with 1

1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday

Please try

select weekday(now()) weekday, dayofweek(now()) dayofweek;

like image 200
Mario Avatar answered Oct 21 '22 04:10

Mario