get Mondays between two dates mysql - mysql

I want to get the Mondays between two dates with a select statement, but I only have searched in sql server.
I have tried this, but it doesn´t work:
SELECT DATE_ADD('2017-01-01', INTERVAL ROW DAY) AS Date
FROM (
SELECT #row := #row + 1 AS row
FROM (
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6
) t1,
(
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6
) t2,
(
SELECT #row:=-1
) t3 LIMIT 31
) b
WHERE DATE_ADD('2017-01-01', INTERVAL ROW DAY) BETWEEN '2017-01-01' AND '2017-12-31'
AND DAYOFWEEK(DATE_ADD('2017-01-01', INTERVAL ROW DAY)) = 2
This query only gives me the Monday´s date for one month.
I hope that you can help me
Regards!

If you really want to solve this SQL only, you have to delve a bit deeper and generate a sequence of days, this is by far the most difficult side task to solve this. Besides this, as you have already guessed, you can use DAYOFWEEK or WEEKDAY to get your desired days.
SELECT *
FROM (
SELECT DATE_ADD('2013-01-01',
INTERVAL n4.num*1000+n3.num*100+n2.num*10+n1.num DAY ) AS DATE
FROM (
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n1,
(
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n2,
(
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n3,
(
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n4
) AS a
WHERE DATE >= '2017-01-01' AND DATE < NOW()
AND WEEKDAY(DATE) = 0
ORDER BY DATE
Some other RDBMS have inbuild functions for that.
If would be way easier if you have a calendar table where all dates are already present.

I think that this solution will work with better performance than previous
SET #date_start = date('2019-07-06');
SET #date_end = date('2019-09-18');
SELECT CASE WHEN MOD(DAYOFWEEK(#date_start), 7) <= 2 AND
MOD(DAYOFWEEK(#date_start), 7) + DATEDIFF(v.selected_date, #date_start) - 7 * FLOOR(DATEDIFF(v.selected_date, #date_start) / 7) >= 2 OR
MOD(DAYOFWEEK(#date_start), 7) > 2 AND
MOD(DAYOFWEEK(#date_start), 7) + DATEDIFF(v.selected_date, #date_start) - 7 * FLOOR(DATEDIFF(v.selected_date, #date_start) / 7) >= 9 THEN 1 ELSE 0 END
+ FLOOR(DATEDIFF(v.selected_date, #date_start) / 7) num_of_mo
, #date_start start_date,v.selected_date end_date
FROM (select adddate(#date_start,t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
WHERE v.selected_date <= #date_end;

Related

Fetch Column from second table using date from first table if the date is in between dates from second table

I have my first table here:
Then i have my second table here:
My problem is that, I needed to get the right corresponding ShiftAssignmentShiftID From second table if the selected_date from first table is in between the start date and end date of the second table. In short, I needed to get/display what shift of the employee is, per day. But I am getting all the ShiftAssignmentShiftID ALL as NULL values.
Here's my query:
SELECT FirstColumns.selected_date,FirstColumns.WeekDay,FirstColumns.EmployeeName,FirstColumns.EmployeeID, tblshiftassignments.ShiftAssignmentShiftID FROM (select EmployeeID,selected_date,CASE WHEN WEEKDAY(selected_date) = 0 THEN 'MON' WHEN WEEKDAY(selected_date) = 1 THEN 'TUES' WHEN WEEKDAY(selected_date) = 2 THEN 'WED' WHEN WEEKDAY(selected_date) = 3 THEN 'THU' WHEN WEEKDAY(selected_date) = 4 THEN 'FRI' WHEN WEEKDAY(selected_date) = 5 THEN 'SAT' WHEN WEEKDAY(selected_date) = 6 THEN 'SUN' END AS 'WeekDay', tblemployee.EmployeeIDDisplay, CONCAT(tblemployee.EmployeeLastName,',',tblemployee.EmployeeFirstName) AS 'EmployeeName' from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v,
tblemployee
where selected_date between '2021-08-01' and '2021-08-30'
order by tblemployee.EmployeeLastName, selected_date) AS FirstColumns LEFT JOIN tblshiftassignments ON tblshiftassignments.ShiftAssignmentEmployeeID = FirstColumns.EmployeeID AND tblshiftassignments.ShiftAssignmentStartDate <= FirstColumns.selected_date AND tblshiftassignments.ShiftAssignmentEndDate + INTERVAL 1 DAY > FirstColumns.selected_date
ORDER BY FirstColumns.EmployeeName,selected_date
Here's my desired result:
Solved! All my queries are correct. The dates that i was testing was 2021 but the data in the table is 2020, i missed this one out. That's why it's returning NULL.

How to Get List of weeks between the given date range?

I want week list with week number, week start date and end date between two dates. Let me give you an example,
If I am passing start date as 2019-12-11 and end date as 2019-12-25, then expected output should look like below:
Week Number | Week start date | Week end date
W1 2019-12-11 2019-12-14
W2 2019-12-15 2019-12-21
W3 2019-12-22 2019-12-25
I have tried using below query but I got output like
Week start date | Week end date
2019-12-15 2019-12-21
2019-12-22 2019-12-28
select start_date, date_add(start_date, INTERVAL 6 DAY) as end_date
from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) start_date
from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where start_date between '2019-12-11' and '2019-12-25'
AND date_format(start_date, '%w') = 0
You can use the following solution using WEEK function and ROW_NUMBER:
-- calendar table: https://stackoverflow.com/a/45992247/3840840
SELECT CONCAT('W', ROW_NUMBER() OVER (ORDER BY WEEK(start_date))) AS `Week Number`,
MIN(start_date) AS `Week start date`, MAX(start_date) AS `Week end date`
FROM (
SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS start_date
FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) v
WHERE start_date BETWEEN '2019-12-11' AND '2019-12-25'
GROUP BY WEEK(start_date)
ORDER BY WEEK(start_date)
By using WEEK you don't have to calculate the weeks yourself. MySQL can detect the weeks. With WEEK you can get the week number of a date value. You can group by this week number to get the rows for every week. With MIN and MAX on the date value you get the first and last date of each week.
With ROW_NUMBER you can add a row number to your result. This function is available since MySQL 8.0. On earlier versions of MySQL you can use a solution like the following:
SELECT CONCAT('W', #row_number:=#row_number+1) AS `Week Number`,
MIN(start_date) AS `Week start date`, MAX(start_date) AS `Week end date`
FROM (
SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS start_date
FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
ORDER BY start_date
) v, (SELECT #row_number:=0) rn
WHERE start_date BETWEEN '2019-12-11' AND '2019-12-25'
GROUP BY WEEK(start_date)
ORDER BY WEEK(start_date)
demo on dbfiddle.uk

Select every other day of week using SQL (MySQL)

The goal is to return a set of dates that correspond to the day of week requested.
For example: every other Monday between 2018-08-13 and 2018-12-31.
The following statement returns all dates and works great
select * from
(select adddate('2010-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) DATES from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where DATES between '2018-08-13' and '2018-12-31'
and dayname(DATES) ='MONDAY'
So to return every other MONDAY I added the following
HAVING DATES % 2 = 0
I assumed that by taking MOD of dates it would, in this case return every MONDAY that met this criteria. Well it does not work. I've tried all sorts of combos but not getting it.
Any ideas?
Dates are a complex data structure and the details of how they are stored and manipulated vary between DBMSs. Most often you can't treat them as numeric data types as you are trying to do here. Instead you should calculate the number of days between the date of interest and a fixed date then you can take the modulus of that number: for example:
select * from
(select adddate('2010-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) DATES from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where DATES between '2018-08-13' and '2019-01-31'
and dayname(DATES) ='MONDAY'
and datediff(dates,'2018-08-13') % 2 = 0
order by dates
Alternatively since every other Monday is 14 days apart you could use modulus 14 and drop the dayname(DATES) ='MONDAY' predicate:
select * from
(select adddate('2010-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) DATES from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where DATES between '2018-08-13' and '2019-01-31'
and datediff(dates,'2018-08-13') % 14 = 0
order by dates
This can be made to work with a few little modifications as follows:
SELECT * FROM
(
SELECT ADDDARE('1970-01-01', t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) v
WHERE
selected_date between '2018-08-08' AND '2018-10-19'
AND DAYOFWEEK(selected_date) = 2
AND WEEK(selected_date) % 2 = WEEK('2018-08-08') % 2
The idea is that it will include the Monday of the week that the selection period begins from. So, it doesn't matter whether the starting week is odd or even, you'd always get alternate Mondays without having to worry about specifying 0 or 1 for the result of modulo operation.

Convert query from MySql to Sqlite

I need to convert this query from MySQL format to SQLite. I'm trying myself but I've found some difficulty.
In SQLite, the curdate() and the interval functions do not exist.
select a.Date
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between '2010-01-20' and '2010-01-24'
What this query actually does is just generating lots of consecutive dates (up to one thousand previous days).
In SQLite 3.8.3 or later, this can be done more easily with a recursive common table expression:
WITH RECURSIVE dates(d)
AS (VALUES('2010-01-20')
UNION ALL
SELECT date(d, '+1 day')
FROM dates
WHERE d < '2010-01-24')
SELECT d AS date FROM dates;
Here is the basic syntax:
select a.Date
from (select date('now', '-'||(a.a + (10 * b.a) + (100 * c.a))|| ' days') as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
) a cross join
(select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
) b cross join
(select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
) c
) a;
I left out the where clause because those dates are more than 1000 days in the past, so they won't select anything anyway.

How to get list of dates between two dates in mysql select query [duplicate]

This question already has answers here:
Get a list of dates between two dates
(23 answers)
Closed 9 years ago.
I want list of dates lies between two dates by select query. For example:
If i give '2012-02-10' and '2012-02-15' I need the result.
date
----------
2012-02-10
2012-02-11
2012-02-12
2012-02-13
2012-02-14
2012-02-15
How can i get?
Try:
select * from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2012-02-10' and '2012-02-15'
-for date ranges up to nearly 300 years in the future.
[Corrected following a suggested edit by UrvishAtSynapse.]
set #i = -1;
SELECT DATE(ADDDATE('2012-02-10', INTERVAL #i:=#i+1 DAY)) AS date FROM `table`
HAVING
#i < DATEDIFF('2012-02-15', '2012-02-10')
This will return your result set exactly as prescribed. This query only requires you change the two different dates in datediff and adddate.
The accepted answer didn't work for me in MySQL 5.5.
I updated the query to work for me:
select * from
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2012-02-10' and '2012-02-15'
Take a look at this post : Get a list of dates between two dates
Check the stored procedure that Ron Savage did, this seems to correspond to what you need !
You can create a table containing all the dates you might ever need to use:
date
2000-01-01
2000-01-02
2000-01-03
...etc..
2100-12-30
2100-12-31
Then query that table as follows:
SELECT date
FROM dates
WHERE date BETWEEN '2012-02-10' AND '2012-02-15'
SELECT * FROM tablexxx WHERE datecol BETWEEN '2012-02-10' AND '2012-02-15';