working days calculation query - working but not able to understand - mysql

I wanted to calculate number of working days for an year (excluding only saturdays and sundays) and got this query from web and its working..but i am not able to understand this query from MID part..can anyone help me to understand this...
SELECT 5 * (DATEDIFF('2015-12-31', '2015-01-01') DIV 7) +
MID('0123444401233334012222340111123400012345001234550', 7 * WEEKDAY('2015-12-31') +
WEEKDAY('2015-01-01') + 1, 1)
i am not able to understand from
MID('0123444401233334012222340111123400012345001234550', 7 * WEEKDAY('2015-12-31') +
WEEKDAY('2015-01-01') + 1, 1)

WEEKDAY('2015-12-31') and WEEKDAY('2015-01-01') are computing the day of the week for the start and end of the year, with 0 = Monday and 6 = Sunday. For the year 2015, these are both 6 (Sunday). So the middle argument of MID is 7 * 6 + 6 + 1, or 49.
The 49th character of the long string is 0. So this means it will add zero to the rest of the expression.
The purpose of this expression is to adjust the weekday count according to what days of the week the first and last day of the year are. In 2015, neither one is a weekday, so it adds zero. Now consider 2016. Jan 1 2016 is a Monday (weekday=1) and Dec 31 2016 is a Tuesday (weekday=2). 7*1+2+1 equals 10. The tenth character of the long string is 1. So it will add one weekday ... intuitively we can see this happens because 2016 is a leap year, so there's one more day in the year. And so on. For each possible beginning and ending day of the year, the long string encodes an adjustment of how many weekdays to add. I imagine that the string was discovered by trial and error.

Related

date calculation with full date in separate columns

I'm trying to make a query for the last 3 months of an item with my month and year in separate columns like so:
YEAR_ PERIOD
2014 5
2013 6
2013 11
2011 6
2009 2
The query needs to always start from the current month and year. I've tried using DateAdd(), DateSerial(), and DateDiff() none of those worked. Whenever I try to use month(now()-3) i'm getting 2 instead of 11.
Adding or subtracting integers and dates simply adds or subtracts days from the date. So Now() - 3 results in 2016-02-15 (it is 2016-02-18 at the time of this posting). That is clearly still the month of February - hence your result of two.
Give this a try Month(DateAdd("m", -3, Now)). Here we are adding -3 months to the current date and then getting the resulting month. Based on today's date that will result in 11.
I figured it out.
DateDiff("m",CDate(Format([PERIOD] & "/" & [YEAR_],"mm/yyyy")),Now())
This took the two fields and made them a single date. I then took the difference from this month and the months between the two dates. I then set the criteria to <= 3.
Addendum
It can be simplified to:
DateDiff("m",CDate([PERIOD] & "/" & [YEAR_]),Date())
In general, however, you should never use string handling for dates if it can be avoided, and it easily can:
DateDiff("m",DateSerial([YEAR_],[PERIOD],1)),Date())

SQL 2008, week numbers and years

Having some problems getting the information I need from the database. I fell foul of the week vs iso_week setting in DATEPART but have now got the problem of getting the year from it. The nub of the issue is that when I run
SELECT datePart(iso_week, '2014-12-29');
SELECT datePart(week, '2014-12-29');
SELECT datePart(YEAR, '2014-12-29');
I get Wk 1 for the first query, Wk 53 for the second. (first being what I want) but the year is still 2014. So if I do
SELECT CONCAT('WK', RIGHT(CONCAT('00', DATEPART(iso_week, '2014-12-29')), 2), ' ', DATEPART(year, '2014-12-29'))
I get WK01 2014 instead of WK01 2015.
Anyone know a way I can get the right ISO week and the associated year for a date?
Edit:
See if I can clear up a bit here.
Not expecting the above to return 2015 instead of 2014, that would be daft. What I need is a way to know that the week number returned is actually in the next year. I did consider doing a straight if week > 52 then its wk 1 in the next year, but that seems a little sledgehammerish and I am not sure it will actually work.
You should understand how the ISO_WEEK is calculated. From Microsoft:
ISO 8601 includes the ISO week-date system, a numbering system for
weeks. Each week is associated with the year in which Thursday occurs.
For example, week 1 of 2004 (2004W01) ran from Monday 29 December 2003
to Sunday, 4 January 2004. The highest week number in a year might be
52 or 53. This style of numbering is typically used in European
countries/regions, but rare elsewhere.
Hence your DATEPART(YEAR, ...) you should pass in the Thursday on or after December 29, 2014.
Try this
SELECT CONCAT('WK', RIGHT(CONCAT('00', DATEPART(iso_week, '2014-12-29') % 53), 2), ' ', DATEPART(year, '2014-12-29') + DATEPART(iso_week, '2014-12-29') % 53)
Had someone set me on the right track with some simple logic. I was trying to over complicate things. This seems to work for everything I can throw at it.
DECLARE #date AS DATETIME
SET #date = '2014-12-29'
SELECT
CAST(
CASE
WHEN MONTH(#Date) = 1 AND DATEPART(iso_week, #date) > 51 THEN YEAR(#Date) - 1
WHEN MONTH(#Date) = 12 AND DATEPART(iso_week, #date) = 1 THEN YEAR(#Date) + 1
ELSE YEAR(#Date)
END
AS VARCHAR(4)) AS Year
,
RIGHT('00' + CAST(DATEPART(iso_week, #date) AS VARCHAR(2)), 2) AS Week
It seems to cover true wk53 occurrences and incorrect ones.

Nth weekday of the month

Pseudo code of what I'd like to do:
SELECT * FROM table WHERE NTH_DAY(DATE(table_date)) = NTH_DAY($input_date);
I want to determine what the nth weekday of the month is for a given date. For example, if given the input "2013-08-30" it should return 5, since it is the fifth occurrence of that weekday (Friday) in the month.
I've been reading through countless similar questions but the majority are looking for just the opposite, for example they want to find the date of the fifth Friday, whereas I want to determine what the nth number of a date is. Other questions appear to be what I'm after but they're in different programming languages I don't understand.
Can someone please explain if this is possible in a MySQL query and how to do it?
To find which "nth" a given day is is rather easy:
select (case when day(table_date) between 1 and 7 then 1
when day(table_date) between 8 and 14 then 2
when day(table_date) between 15 and 21 then 3
when day(table_date) between 22 and 28 then 4
else 5
end) as NthDay
You can also do this using "remainder" arithmetic:
select 1 + floor((day(table_date) - 1) / 7) ) as NthDay
I'm going to throw my two cents in here with a third option that gets it based on finding the 1st of each day, and then adding multiples of 7 to get to the final result.
Function
CREATE FUNCTION `ISNTHDAYINMONTH`(checkDate DATE, weekNumber INTEGER, dayOfWeek INTEGER, monthOfYear INTEGER) RETURNS INTEGER
NO SQL
DETERMINISTIC
BEGIN
DECLARE firstOfMonth DATE;
SET firstOfMonth = DATE_SUB(checkDate, INTERVAL DAYOFMONTH(checkDate) - 1 DAY); #Find the first day of the current month
IF DAYOFWEEK(checkDate) = dayOfWeek AND MONTH(checkDate) = monthOfYear #Make sure at least this matches
AND DAYOFMONTH(checkDate) = ((1 + (7 + dayOfWeek - DAYOFWEEK(firstOfMonth)) % 7) + 7 * (weekNumber - 1)) THEN #When the date matches the nth dayOfWeek day of the month
RETURN 1;
ELSE
RETURN 0; #Nope
END IF;
END
Use
SELECT ISNTHDAYINMONTH(datecol, weekNumber, dayOfWeek, monthOfYear);
Where weekNumber is 1 through 5, dayOfWeek is 1 through 7 (1 = Sun), and monthOfYear is 1 through 12.
Example
To check if datecol is the second Tuesday of April:
SELECT ISNTHDAYINMONTH(datecol, 2, 3, 4);
Calculations
Let's break it down. This line gets the first day of the month for the date that is passed in.
SET firstOfMonth = DATE_SUB(checkDate, INTERVAL DAYOFMONTH(checkDate) - 1 DAY); #Find the first day of the current month
This check ensures that the date has the correct day of the week, and correct month (just in case the user passed in a date that isn't even the right day of the week).
DAYOFWEEK(checkDate) = dayOfWeek AND MONTH(checkDate) = monthOfYear
Now for the big one:
DAYOFMONTH(checkDate) = ((1 + (7 + dayOfWeek - DAYOFWEEK(firstOfMonth)) % 7) + 7 * (weekNumber - 1))
To get the amount we need to add to 1 to get the date, we calculate dayOfWeek that we are looking at, minus the day of the week that the first day of the month falls on to get the offset mod 7 ((dayOfWeek - DayOfWeek(first)) % 7).
Note that because the first of the month could land on a day before or on the day we are looking at, we add an additional 7 and then mod by seven. This is needed because MySQL's Mod function does not properly compute mod. I.e. -1 % 7 should be 6, but MySQL returns -1. Adding 7 and taking the modulus ensures the result will always be correct.
To summarize:
NumberToAddToOneToGetDateOfFirstWeekDay = (7 + DayOfWeek - DayOfWeek(firstDayOfMonth)) %
Then we add one and how every many multiples of 7 are needed to get to the correct week.
select ceiling(DATEPART(day, GETDATE())/7.0)
This will always give you nth occurrence number of current day in a month.
For example, if the current day is Friday, and it occurred 3rd time this month. It will return 3.
If you replace GETDATE() with your Date variable or column name it will return the occurrence of the day on that date in the corresponding month.
This would give the number of the week (which is actually the same as if it is the 5th friday or 5th Monday)
SELECT WEEK(dateasd,5) -
WEEK(DATE_SUB(dateasd, INTERVAL DAYOFMONTH(dateasd)-1 DAY),5)
from test
Use Weekday() function to find which day it is
Fiddle at
http://www.sqlfiddle.com/#!2/7a8b6/2/0

Week of the year for weeks starting with Saturday

We have customers that currently have defined weeks starting either on Sat, Sun or Monday. Came across these DATE_FORMAT options which nicely handle the week starting on Sunday and Monday but can't find a way to do same for week starting on Saturday. Any suggestions?
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
I had a similar issue: I needed to calculate week numbers based on the following rules:
Week starts on Friday
The remainder days of an year (all the days after the last Friday of the year that do not complete a week) should be counted in the
first week of the next year.
For example:
27/12/2012 (Thursday) should be Week 52 of 2012
28/12/2012 (Friday) should be Week 1 of 2013
Week 1 2013 goes from 28/12/2012 to 3/1/2013
I made this statement that calculates both the YEAR and WEEKNUMBER based on these rules that you can easily adapt to your circunstance:
SELECT IF(ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7) > 52, YEAR(current_date)+1, YEAR(current_date)),
IF(ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7) > 52, 1, ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7));
The tricky part is just this expression:
ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7)
The rest (If clauses) are just for adapting the result of the expression to make year+1 and week = 1 on week 53.
I'll try to explain the expression as best as I can. The following expression gives you the week number pure simple (the day of the year divided by 7 days in a week rounded up):
ceil(( dayofyear(current_date))/7)
But now you want to make it start on Friday (or any other day). To do this you need to add to the current day, the days of the first week that were part of the previous year (it's like your current actually started a few days before, because your first week contains days from the previous year).
This expression calculates that offset based on the weekday on Jan/1:
dayofweek(date_format(current_date, '%Y-01-01'))+OFFSET
The offset is the difference between 7 and the weekdaynumber you want the week to start:
0 for Saturday
1 for Friday
2 for Thursday
3 for Wednesday
...
So now you just have to add it to the previous one resulting in the above mentioned expression that calculates the week numbers starting on any weekday and assuming week 1 to start on the previous year:
ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+OFFSET )/7)
Then I just added an IF that turns week 53 into week 1 and another to add 1 to the year if it's week 53.
It took me a while to think on this question.
ISO standard defines the first week to start on Monday and contain 4th day of the year.
MySQL's functions provide much more choices.
date_format() flags %U and %u are using notation where first week is the one where Sunday or Monday is first met. As this is not according to the ISO, I will provide both variants.
If you want to count week numbers starting from Saturday and first year's week is the one containing Saturday, you can use one of the following expressions:
SELECT sign(dayofweek(current_date) - 7) + ceil(dayofyear(current_date)/7);
SELECT ceil((dayofyear(current_date)+
(dayofweek(date_format(current_date, '%Y-01-01'))%7-7))/7);
If first year's week is the one where 4th day of the year falls into, use:
SELECT ceil((dayofyear(current_date)+
(dayofweek(date_format(current_date, '%Y-01-04'))%7-4+1))/7);
The very first expression is quite straightforward.
I will elaborate on the 2nd and 3rd ones. I calculate week number by taking current day of the year, dividing by 7 and ceiling up, quite simple. Week number needs to be adjusted based on the situation at the beginning of the year though.
for the first case (first week starts with the first Saturday), I take day-of-week for the Jan/1 of the year in subject, make Saturday as the day 0 and then adjust day-of-year by the difference. This this makes all days before first saturday yielding negative adjustment number and it ceils up to zero;
for the second case (first week is the one where 4 day of the year falls in), I take day-of-week for the Jan/4 of the year in subject, make Saturday as the day 0. The -4+1 formula gives adjustment to the first Saturday before Jan/4, +1 is used as days of the year starts from 1, not from 0. Negative adjustment means 1st day of the year is not in the first week of the year.
Here're some test dates on the SQL Fiddle.
If you want to count weeks from any other day, you just have to change the formula, making that day being 0 in the sequence. Say, to count weeks starting from Wednesday, use:
SELECT ceil((dayofyear(current_date)+
((dayofweek(date_format(current_date, '%Y-01-04'))+3)%7-4+1))/7);
+3 is used as it complements dayofweek() value for Wednesday to the 7.
Make an adjustment based on DAYOFWEEK().

Get name of day from month, year, and day

Does anyone know a way to convert a month, year, and day into the day's name for any year? Example:
function convert(day, year, month)
...
return "Monday"
end
Thanks in advance!
You can use the following method:
This method uses codes for different
months and years to speed up the
calculation of the day of the week.
You might even be able to memorize the
codes. We'll use December 16, 2482 as
an example.
Take the last 2 digits of the year. In
our example, this is 82.
Divide by 4, and drop any remainder.
82 / 4 = 20, remainder 2, so we think
"20."
Add the day of the month. In our
example, 20 + 16 = 36.
Add the month's key value, from the
following table. Jan Feb Mar Apr May
June July Aug Sept Oct Nov Dec 1 4 4
0 2 5 0 3 6 1 4 6
The month for our example is December,
with a key value of 6. 36 + 6 = 42.
If your date is in January or February
of a leap year, subtract 1. We're
using December, so we don't have to
worry about this step.
Add the century code from the
following table. (These codes are for
the Gregorian calendar. The rule's
slightly simpler for Julian dates.)
1700s 1800s 1900s 2000s 4 2 0 6
Our example year is 2482, and the
2400s aren't in the table. Luckily,
the Gregorian calendar repeats every
four hundred years. All we have to do
is add or subtract 400 until we have a
date that is in the table. 2482 - 400
= 2082, so we look at the table for the 2000s, and get the code 6. Now we
add this to our running total: 42 + 6
= 48.
Add the last two digits of the year.
48 + 82 = 130.
Divide by 7 and take the remainder.
This time, 1 means Sunday, 2 means
Monday, and so on. A remainder of 0
means Saturday.
How to calculate the day of the week
A quickl google for "day of week from date algorithm" showed up this Wikipedia article
But depending on the dates you need to work with, beware the strange history of Gregorian calendar adoption