I am trying to write a query which outputs weekly dates from a given date to the end of the current year.
I am stuck here:
Select DATE_ADD(input_date, INTERVAL 7 DAY)
as alarm_date from userInput;
How do I get all the dates?
WITH RECURSIVE
cte AS ( SELECT id, input_date + INTERVAL 7 DAY alarm_date FROM userInput
UNION ALL
SELECT id, alarm_date + INTERVAL 7 DAY FROM cte WHERE YEAR(alarm_date) = YEAR(NOW()) )
SELECT * FROM cte;
Related
I am looking for a query in MYSQL that would allow me to obtain the equivalent date for each month of the current year from old dates so for example The date: '2005-01-31' I would like to see the following populated into 12 separate month fields:
Jan - '2021-01-31',
Feb - '2021-02-28',
Mar - '2021-03-31'
I have attempted the following query however this only populates the same month of the old date but does however show the current equivalent day and year:
Select DATE_FORMAT(DATE_ADD('2005-01-31', INTERVAL (YEAR(CURRENT_DATE()) - YEAR('2005-01-31') ) YEAR), '%Y-%m-%d') `date`;
'2021-01-31'
An example for a few months would be much appreciated and I should be able to adapt for the rest of the calendar year myself.
WITH RECURSIVE
cte AS ( SELECT 0 n
UNION ALL
SELECT n + 1 FROM cte WHERE n < 11 )
SELECT DATE_FORMAT(#date, '2021-%m-%d') + INTERVAL n MONTH `date`
FROM cte
fiddle
does not work for me on MySQL 5.x
SELECT DATE_FORMAT(#date, '2021-%m-%d') + INTERVAL n MONTH `date`
FROM (SELECT 0 n 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 UNION SELECT 10 UNION SELECT 11 ) cte
fiddle
I need to calculate the hours between two dates, one of them being NOW(), excluding weekends.
I have found a few solutions that use stored procedures or mysql functions, but i'm looking to do this within a single query.
I can get the general hour difference by using this:
SELECT HOUR(TIMEDIFF(NOW(), created_at))
But I need this to only return hours of weekdays, not weekends. So from Friday at 23:00 to Monday at 1:00 there should only be a 2 hour difference.
Any help would be appreciated.
Thank you!
Mysql 8
In a programming language you'd write a loop and sum up the hours by iterating through the days. In SQL you can do the same with a recursive query:
with recursive cte (id, dt, minutes) as
(
select
id,
date(created_at),
case
when dayofweek(created_at) in (1, 7) then 0
else timestampdiff(minute, created_at, least(now(), date(created_at) + interval 1 day))
end
from mytable
union all
select
id,
dt + interval 1 day,
case
when dayofweek(dt + interval 1 day) in (1, 7) then 0
else timestampdiff(minute, dt + interval 1 day, least(now(), dt + interval 2 day))
end
from cte
where dt + interval 1 day <= curdate()
)
select id, round(sum(minutes) / 60, 1) as hours
from cte
group by id
order by id;
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a08f96bc29fb8a51302da10679496e22
There are a method with SQL to SELECT from one database the date records where the date is today and the date is multiple of two years.
For example, i have a table call "list". That table have two column, 'ID' and 'last_date'. One of this record is: ID = '1' and date = '17-03-2015'
I need to select all record where the date is the date on the table + 2 year. For example today the query will return the ID 1.
Thanks to all.
Use DATE_SUB() to subtract 2 years from today's date, and compare that to the column.
SELECT id
FROM list
WHERE last_date = DATE_SUB(CURDATE(), INTERVAL 2 YEAR);
This is a little better than #Teja's solution because it only has to do the date arithmetic once, rather than for every row in the table. And if there's an index on the last_date column, it will be able to use it to find the rows quickly.
We can write an expression that returns a date value that is exactly two days before today's date:
SELECT DATE(NOW()) + INTERVAL -2 YEAR
We can use an expression in the WHERE clause of a query. If the column in the table we want to check is defined as DATE datatype:
SELECT t.id
FROM mytable t
WHERE t.mydatecol = DATE(NOW()) + INTERVAL -2 YEAR
If it's defined as a DATETIME, the normal pattern would be range check
SELECT t.id
FROM mytable t
WHERE t.mydatecol >= DATE(NOW()) + INTERVAL -2 YEAR
AND t.mydatecol < DATE(NOW()) + INTERVAL -2 YEAR + INTERVAL 1 DAY
If the column is stored as a VARCHAR in a non-canonical format e.g. DD-MM-YYYY then we could either attempt to convert that to a DATE using STR_TO_DATE (which we don't like to do because the query can't make effective use of a index), or we could convert our generated date value into the required string format for an equality comparison:
SELECT t.id
FROM mytable t
WHERE t.ddmmyyyy = DATE_FORMAT(DATE(NOW()) + INTERVAL -2 YEAR,'%d-%m-%Y')
That would get us exact match to '17-03-2015', but not to '17-3-2015'. And we have to do equality test or IN list, we can't do range check, because the value stored in the column isn't canonical.
If we need to look for multiple dates... today, two years ago, four years ago, six years ago, ... we can generate a list of dates and perform a join operation. (Assuming that mydatecol is defined as DATETIME...)
SELECT t.id
FROM ( SELECT DATE(NOW()) + INTERVAL 0 YEAR AS dt
UNION ALL SELECT DATE(NOW()) + INTERVAL -2 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -4 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -6 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -8 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -10 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -12 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -14 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -16 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -18 YEAR
UNION ALL SELECT DATE(NOW()) + INTERVAL -20 YEAR
) l
JOIN mytable t
WHERE t.mydatecol >= l.dt + INTERVAL 0 DAY
AND t.mydatecol < l.dt + INTERVAL 1 DAY
I want to get the three working days from the current date as excluding Saturday and Sunday. can any one help me out here.
I have tried the interval method and DayOfWeek(day) <> 1 AND DayOfWeek(day) <> 7 but it is not giving me the proper result
Not very elegant but
select d
from
(
select curdate() as d
union all
select curdate() + interval 1 day
union all
select curdate() + interval 2 day
union all
select curdate() + interval 3 day
union all
select curdate() + interval 4 day
) tmp
where dayofweek(d) not in (1,7)
order by d
limit 3
what will be the smartest way to select all rows from MySQL table for the past 3 months if the table has the following columns:
| id (int) | year (int)| month (int) |
Considering that if the current month & year are for example 2.2016 I need to select all records for 11.2015 & 12.2015 & 1.2016
It is easy if the current month is greater than 3 because all months that I need to select are in the same year so I can subtract 3 from the current month and run simple query
SELECT * FROM mytabe where year=2016 and month >= xx
Select * from mytable where STR_TO_DATE(concat(year,"-",month,"-01"),'%Y-%m-%d')>date_sub(curdate(),Interval 3 month) ;
The above query will get fetch year and month from date 3 months before today
You can select three Months records by these queries follow this.
The columnName means which column data want you select.
The tableName means which table data want you select.
The dateColumnName means which column date base you want to select data.
It would return Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 1 MONTH) AND Date(Now())
It would return Second Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 2 MONTH) AND ( DATE(NOW()) - INTERVAL 1 MONTH)
It would return Third Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 3 MONTH) AND ( DATE(NOW()) - INTERVAL 2 MONTH)
May It help to others.
Please try this
Select * from mytable where STR_TO_DATE(concat(year,"-",month,"-01"),'%Y-%m-%d')>date_sub(curdate(),Interval 3 month) ;