I have sum of all week sales into one object and object need to display in charts and all chart need to have date in x-axis so, Here I am calculating date from year+week based on Monday
SELECT STR_TO_DATE(CONCAT('20131',' Monday'), '%X%V %W');
I want it based on first day on year
SELECT DAYNAME(MAKEDATE(2013,1))
All togather
SELECT STR_TO_DATE(CONCAT(CONCAT(2013, 1), DAYNAME(MAKEDATE(2013,1))), '%X%V %W')
return '2013-01-08' which is bad value I need '2013-01-01'
SELECT STR_TO_DATE(CONCAT(CONCAT(2017, 1), DAYNAME(MAKEDATE(2017,1))), '%X%V %W')
here it return '2017-01-01' which is good
My problem is in some years it went to next week which is bad I am not sure where and what I am doing wrong ... So Please any kind of help...
Problem:
In some year like 2013 when I convert year+ week to date I get date in one extra week like 2013year+ 1week gives 2013-01-08 which is in second week instead of first
but
In some year like 2017 when I convert year+ week to date I get date in right week like 2017year+ 1week gives 2017-01-01
Why is this behavior?
MySQL supports intervals which seems to be the best feature for creating a date from a year and week number. See the following query as an example which calculates the first week of 2013 and the second week of 2013.
SELECT DATE_ADD(makedate(`year`, 1), INTERVAL (`week` - 1) WEEK) as weekdate
FROM (
(SELECT 2013 as `year`, 1 as `week`) UNION
(SELECT 2013 as `year`, 2 as `week`)
) t1
The output is two rows
weekdate
2013-01-01
2013-01-08
The following also works but is far less easy to read
SELECT MAKEDATE(`year`, 1 + ((`week` - 1) * 7)) as weekdate
FROM (
(SELECT 2013 as `year`, 1 as `week`) UNION
(SELECT 2013 as `year`, 2 as `week`)
) t1
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.