Get nth previous working date in MySQL - mysql

How do I get the nth previous working date in MySQL?
Suppose the function signature is
nth_previous_working_date (d DATE, n INT)
For 10th September, 2015, the function should return 3rd September, 2015

You can use the date_sub function.
SELECT DATE_SUB('2015-09-10', INTERVAL 7 DAY);
or simply use the minus operator
SELECT '2015-09-10' - INTERVAL 7 DAY
See the working example.

Getting the previous non-weekend working day can be done with a case.
select case dayofweek(curdate() - interval 1 day) -- yesterday
when 1 then curdate() - interval 3 day -- when sunday, go 2 more days before
when 7 then curdate() - interval 2 day -- when saturday, go 1 more days before
else curdate() - interval 1 day -- yesterday
end

Related

Query of current date in time mysql

I have a column of time with type datetime in mysql called createdAt, so I want find out the data with time range like this
createdAt >= SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate()))
and createdAt <= ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate()))
assume createdAt curdate() based on this question made are october 8th 2020, so that time range will be 28th september 2020 until 04th october 2020
But when I check manually, if I add some time range in another media, why the counting is little bit more different? For example: using this query, count of transaction is 87 transaction, but then I used another media to count the transaction between 28th september 2020 00:00:00 until 04th october 23:59:59 its 90 transaction, is that any I can add in query for time range in my query date range?
Your expression ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate())) returns only date part of last week day, so all data after start this day is not included.
You have couple of options to solve this:
Round createdAt to date withot time part:
SELECT *
FROM your_table
WHERE DATE(createdAt) BETWEEN
SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate())) AND
ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate()));
Use strong less condition with next day:
SELECT *
FROM your_table
WHERE
createdAt >= SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate())) AND
createdAt < ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 7-WEEKDAY(curdate()));

How to query for the first date of a week on the PREVIOUS year

Wracking my brain trying to figure this out, and I can't seem to find any existing threads that help.
Simply, I'd like to find the first day of the week (as a date) but one year ago, for any given date. Our calendar week starts on Sunday.
Here's a snap of the table I have at my disposal
Any help is greatly appreciated!
Thanks!
See this. How do I get the first day of the week of a date in mysql?
You can get your required result this way:
mydate - INTERVAL 1 YEAR + INTERVAL 1-DAYOFWEEK(mydate - INTERVAL 1 YEAR) DAY
Explanation:
mydate - INTERVAL 1 YEAR
gives you the date a year before mydate.
anyday + INTERVAL 1-DAYOFWEEK(anyday) DAY
gives you the Sunday beginning the week of anyday.
Similarly you can get the first day of the month of anyday like this:
LAST_DAY(anyday) + INTERVAL 1 DAY - INTERVAL 1 MONTH
Some people call this week- and month- truncation.
You can use datesub() to subtract one year from today (curdate()). Get the weekday with weekday(). It returns a number from 0 to 6 where 0 is Monday. Subtract that many days plus one, as your first weekday is Sunday not Monday.
date_sub(date_sub(curdate(),
INTERVAL 1 YEAR),
INTERVAL weekday(date_sub(curdate(),
INTERVAL 1 YEAR)) + 1 DAY)
Here's a function to do it:
CREATE FUNCTION `SUNDAY`(indate date) RETURNS date
NO SQL
BEGIN
declare prevyear date;
set prevyear = indate - interval 1 year;
return prevyear - weekday(prevyear) - interval 1 day;
END

select specific time from previous day

I basically want to select data between 7pm the previous day and NOW(). I'm not sure the best practice or most efficient way to do this on an automated report generated by a query I could write.
SELECT * FROM table WHERE timestamp BETWEEN curdate() - INTERVAL 1 DAY AND
NOW()
How do I get the curdate() - interval 1 day to start at 7pm of the previous day?
curdate() returns just the date, e.g. 2018-03-21. Time in this case is omitted, but would be 00:00:00. To start at 07:00 pm just add another 19 hours like this:
select curdate() - INTERVAL 1 DAY + INTERVAL 19 HOUR;

How to get last Sunday of previous year, if we enter any date in MySQL?

Ex. If I enter today's date 2014-06-23 it should show me previous year's last Sunday date i.e. 2013-12-29. Should not use any procedure / sub-queries, it should be only a single query. Im using MySQL. Kindly help.
Try something like this:
select (date(now()) - interval dayofweek(now()) - 1 day) - interval (weekofyear(now()) - 1) * 7 day
This will return last year Sunday of Current Month
SELECT DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 year) last_sun
Fiddle Demo
This will return Last year last sunday
SELECT (DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 day))- INTERVAL (WEEKOFYEAR(NOW()) - 1) * 7 DAY AS Sunday
Fiddle Demo

How do I get the first day of the week of a date in mysql?

Suppose I have 2011-01-03 and I want to get the first of the week, which is sunday, which is 2011-01-02, how do I go about doing that?
The reason is I have this query:
select
YEAR(date_entered) as year,
date(date_entered) as week, <-------This is what I want to change to select the first day of the week.
SUM(1) as total_ncrs,
SUM(case when orgin = picked_up_at then 1 else 0 end) as ncrs_caught_at_station
from sugarcrm2.ncr_ncr
where
sugarcrm2.ncr_ncr.date_entered > date('2011-01-01')
and orgin in(
'Silkscreen',
'Brake',
'Assembly',
'Welding',
'Machining',
'2000W Laser',
'Paint Booth 1',
'Paint Prep',
'Packaging',
'PEM',
'Deburr',
'Laser ',
'Paint Booth 2',
'Toolpath'
)
and date_entered is not null
and orgin is not null
AND(grading = 'Minor' or grading = 'Major')
and week(date_entered) > week(current_timestamp) -20
group by year, week(date_entered)
order by year asc, week asc
And yes, I realize that origin is spelled wrong but it was here before I was so I can't correct it as too many internal apps reference it.
So, I am grouping by weeks but I want this to populate my chart, so I can't have all the beginning of weeks looking like different dates. How do I fix this?
If the week starts on Sunday do this:
DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY)
If the week starts on Monday do this:
DATE_ADD(mydate, INTERVAL(-WEEKDAY(mydate)) DAY);
more info
If you need to handle weeks which start on Mondays, you could also do it that way. First define a custom FIRST_DAY_OF_WEEK function:
DELIMITER ;;
CREATE FUNCTION FIRST_DAY_OF_WEEK(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN SUBDATE(day, WEEKDAY(day));
END;;
DELIMITER ;
And then you could do:
SELECT FIRST_DAY_OF_WEEK('2011-01-03');
For your information, MySQL provides two different functions to retrieve the first day of a week. There is DAYOFWEEK:
Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). These index values correspond to the ODBC standard.
And WEEKDAY:
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
If week starts on Monday
SELECT SUBDATE(mydate, weekday(mydate));
If week starts on Sunday
SELECT SUBDATE(mydate, dayofweek(mydate) - 1);
Example:
SELECT SUBDATE('2018-04-11', weekday('2018-04-11'));
2018-04-09
SELECT SUBDATE('2018-04-11', dayofweek('2018-04-11') - 1);
2018-04-08
Week starts day from sunday then get First date of the Week and Last date of week
SELECT
DATE("2019-03-31" + INTERVAL (1 - DAYOFWEEK("2019-03-31")) DAY) as start_date,
DATE("2019-03-31" + INTERVAL (7 - DAYOFWEEK("2019-03-31")) DAY) as end_date
Week starts day from Monday then get First date of the Week and Last date of week
SELECT
DATE("2019-03-31" + INTERVAL ( - WEEKDAY("2019-03-31")) DAY) as start_date,
DATE("2019-03-31" + INTERVAL (6 - WEEKDAY("2019-03-31")) DAY) as end_date
select '2011-01-03' - INTERVAL (WEEKDAY('2011-01-03')+1) DAY;
returns the date of the first day of week. You may look into it.
This is a much simpler approach than writing a function to determine the first day of a week.
Some variants would be such as
SELECT DATE_ADD((SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1)DAY),INTERVAL 7 DAY) (for the ending date of a query, such as between "beginning date" and "ending date").
SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) DAY (for the beginning date of a query).
This will return all values for the current week. An example query would be as follows:
SELECT b.foo FROM bar b
WHERE b.datefield BETWEEN
(SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) DAY)
AND (SELECT DATE_ADD((SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1)DAY),INTERVAL 7 DAY))
This works form me
Just make sure both dates in the below query are the same...
SELECT ('2017-10-07' - INTERVAL WEEKDAY('2017-10-07') Day) As `mondaythisweek`
This query returns: 2017-10-02 which is a monday,
But if your first day is sunday, then just subtract a day from the result of this and wallah!
If the week starts on Monday do this:
DATE_SUB(mydate, INTERVAL WEEKDAY(mydate) DAY)
SELECT MIN(DATE*given_date*) FROM *table_name*
This will return when the week started at for any given date.
Keep the good work going!