I am trying to figure out the syntax required to replicate the excel function of weeknum([date],16) that will achieve the same results in MySQL.
Currently, EXCEL WEEKNUM 16
=WEEKNUM(DATE(2021,1,1),16) = 1
but all of the week functions in MySQL cannot replicate this, I also tried
select week('2021-01-01', 0) from dual; result = 0
select week('2021-01-01', 1) from dual; result = 0
select week('2021-01-01', 2) from dual; result = 52
select week('2021-01-01', 3) from dual; result = 53
select week('2021-01-01', 4) from dual; result = 0
select week('2021-01-01', 5) from dual; result = 0
select week('2021-01-01', 6) from dual; result = 53
select week('2021-01-01', 7) from dual; result = 52
so as of now, my only option is to use CASE statements and manually +/- week numbers to match; however, this does not work for previous years when (again) trying to match to Excel WEEKNUM 16.
There is currently a question and answer on StackOverflow: MySQL week() Function to begin with Saturday asking the very same question; however, I have tried the solution presented and it does not work.
any help would be appreciated!
Yes as second parameters it only accepts numbers from 0 to 7
In Excel you can enter the days when the year starts but that is not like the standard works, but mysql folows the iso standard https://en.wikipedia.org/wiki/ISO_week_date
For that what you want you need a function
SELECT WEEK( '2021-01-01', 0 )
| WEEK( '2021-01-01', 0 ) | | -----------------------: | | 0 |
SELECT WEEK( '2021-01-01', 1 )
| WEEK( '2021-01-01', 1 ) | | -----------------------: | | 0 |
SELECT WEEK( '2021-01-01', 2)
| WEEK( '2021-01-01', 2) | | ----------------------: | | 52 |
SELECT WEEK( '2021-01-01', 3 )
| WEEK( '2021-01-01', 3 ) | | -----------------------: | | 53 |
SELECT WEEK( '2021-01-01', 4 )
| WEEK( '2021-01-01', 4 ) | | -----------------------: | | 0 |
SELECT WEEK( '2021-01-01', 5 )
| WEEK( '2021-01-01', 5 ) | | -----------------------: | | 0 |
SELECT WEEK( '2021-01-01', 6 )
| WEEK( '2021-01-01', 6 ) | | -----------------------: | | 53 |
SELECT WEEK( '2021-01-01', 7 )
| WEEK( '2021-01-01', 7 ) | | -----------------------: | | 52 |
db<>fiddle here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.