First day of the week and the calendar week - formula

I've to do a bit calc work and need to know how I can get the first day of the current week and the calendar week. It's for my training certificate.

Assume cell A1 contains the formula =TODAY()
The calendar week number is =WEEKNUM(A1).
Assuming Sunday is the first day of the week, the first day of the current week can be computed using =A1-WEEKDAY(A1)+1

Thanks to #Bathsheba, #AxelRichter and more.
The function depends on which language you have set in Open Office. In my OO its german so I'll let my calculation for the first and last day of a work week here:
Monday: =DATUM(JAHR(HEUTE());MONAT(HEUTE());TAGEIMMONAT(MONAT(12)) - ( TAG(HEUTE()) - WOCHENTAG(HEUTE();2)) )
Friday: =DATUM(JAHR(HEUTE());MONAT(HEUTE());TAGEIMMONAT(MONAT(12)) - ( TAG(HEUTE()) - WOCHENTAG(HEUTE();2) - 4) )
So you just have to translate into your language.
Hope this helps others too.

Related

Spotfire change datetime column to monday of weekday

im using spotfire software and i have a datetime column like:
DateTime
21/07/2022 12:11:01
21/07/2022 14:32:01
04/12/2022 10:22:01
30/06/2022 16:22:01
how can i created a new calculated column where it instead changes the date to the monday of the week for all values?
many thanks
i can do this in power bi by dropping the time and using the function
Start of Week Monday = 'Data'[DateTime ]+1-WEEKDAY('Data'[DateTime ]-1)
this results in all the values changing to the monday of its given week/month/year
how can i do this in spotfire?
Assuming you have the following functions available to you: DateAdd, DayOfWeek (*)
You could try:
DateAdd("dd",1 - (If(DayOfWeek([original_Date])=0,7,DayOfWeek([original_Date]))),[original_Date])
This resets the date (including the time portion) to the previous Monday.
Initially I had tried the simpler formula:
DateAdd("dd",1 - DayOfWeek([original_Date]),[original_Date])
but Sunday is mapped to a zero, so whenever the original day was a Sunday, it was pushed to the following Monday.
It does depend on your original settings so you may have to play around with the numbers a bit.
(*) depending on what the source of your data is, equivalent functions may be available.

VB or macro to exclude period of times from time duration calculation in Excel

I have an Excel table which contains thousands of incident tickets. Each tickets typically carried over few hours or few days, and I usually calculate the total duration by substracting opening date and time from closing date and time.
However I would like to take into account and not count the out of office hours (night time), week-ends and holidays.
I have therefore created two additional reference tables, one which contains the non-working hours (eg everyday after 7pm until 7am in the morning, saturday and sunday all day, and list of public holidays).
Now I need to find some sort of VB macro that would automatically calculate each ticket "real duration" by removing from the total ticket time any time that would fall under that list.
I had a look around this website and other forums, however I could not find what I am looking for. If someone can help me achieve this, I would be extremely grateful.
Best regards,
Alex
You can use the NETWORKDAYS function to calculate the number of working days in the interval. Actually you seem to be perfectly set up for it: it takes start date, end date and a pointer to a range of holidays. By default it counts all days non-weekend.
For calculating the intraday time, you will need some additional magic. assuming that tickets are only opened and closed in bussines hours, it would look like this:
first_day_hrs := dayend - ticketstart
last_day_hrs := ticketend - daystart
inbeetween_hrs := (NETWORKDAYS(ticketstart, ticketend, rng_holidays) - 2) * (dayend - daystart)
total_hrs := first_day_hrs + inbetween_hrs + last_day_hrs
Of course the names should in reality refer to Excel cells. I recommend using lists and/or names.

PL/SQL group by week

In MySQL I am using
week(date,3)
to get correct values of week grouping.
How to translate this to PL/SQL? I'v tried the following but what about this 3 from week function in mysql?
TRUNC (created_dt, 'IW')
Oracle is using the NLS setting of the database to determinate how the week number should be calculated and therefor there is no need (according to Oracle) for the '3' part pf the MySQL function. I can image that there still should be useful to have this option but this is once again a sign of the fact that Oracle does not fully understand the needs of working outside USA.
Based on your MYSQL statment above, it returns the week of the specified date e.g. 2012-12-07, 3 as the second argument defines that the third day of the week is assumed as Monday...
If you look at this article it says there are 8 ways MYSQL WEEK() function can behave. So you gotta let us know what results you are trying to achieve by looking for MYSQL Week equivalent function in PL/SQL.
In most staright forward manner, MYSQL WEEK(date[mode]) returns the week number for a given date.
From re-reading your question, the only thing I grabbed that you want to achieve the first feature within PL/SQL and so you are looking for an equivalent function.
And with Oracle it gets slightly RAMEN...
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh. It goes on with how the year starts. E.g. 2012 started on a Sunday, ORACLE THINKS.............. that weeks are Sundays to Saturday.
iw Week of year (1-52, 1-53) based on the ISO standard. Hence the weeks do not necessarily start on Sunday.
WW Week of the year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
By default Oracle NLS settings are set to following:
NLS_CALENDAR : Gregorian
NLS_DATE_LANGUAGE: AMERICAN
NLS_TERRITORY: AMERICA
So you can suggest Oracle to follow the calendar by manipulating your query level....
You could something like this:
select trunc('2012-12-07','YY') AS week_number,
to_number(to_char(trunc('2012-12-07','YY')+rownum-1,'D')) AS dayNumber_in_week
from dual connect by level <= 365
where to_char('2012-12-07','IW') = 3
and to_char('2012-12-07','DY') = 'MON';

How do I get the last day on the month using SQL Reporting Services

In SQL Server Reporting Services, how would I calculate the last day of the current month?
Here is the answer I came up with
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)
As it took me a while to Figure this out, and JC's answer was the simplest to modify and had a good logical structure. I expanded it Slightly for others searching for answers on this topic.
I have found normally you don't just want the Last Day of a month / year you also want the first day of the month / year. So here they are the full lines to calculate just that. wtih the SQl version there also.
First Day of Current month
SSRS=Today.AddDays(1-Today.Day)
SQL=SELECT DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,getdate()),0))
Last day of Current Month
SSRS=Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
SQL=SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
First Day of Current year
SSRS=Today.AddMonths(1-Today.month).AddDays(1-Today.day)
SQL=SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
Last Day of Current Year
SSRS=Today.AddDays(1-Today.Day).AddMonths(13-today.month).AddDays(-1)
SQL=SELECT DATEADD(dd,-1,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0)))
I hope this helps somone.
I know you've found your own answer, but I'd suggest this alternative:
=Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
It's a little easier to read, in my opinion, and might have slightly better performance (though most likely unnoticeable)
And, of course, if you wanted to pad out that date to 23:59:59, as is often necessary, just modify slightly:
=Today.AddDays(1-Today.Day).AddMonths(1).AddSeconds(-1)
From the blog of a Microsoft SQL Team member:
-- returns the last day of the current month.
select dbo.Date(year(getdate()), month(getdate())+1,0)
http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx
Hope this helps!
--Dubs
You may try this expression, Just replace now with your date field.
=DateSerial(Year(Now), Month(Now), 1)
Hope this helps.
Regards
There is an even easier way as in the marked answer:
=DateSerial(Year(Now()), Month(Now())+1, 0)
Since DateSerial() accepts integers as parameters one does not have to use AddMonths() and AddDays(). As in the example an instant calculation inside DateSerial() is possible.
Furthermore day 1 is the first day of the month while the first day minus one day is the last day of the month before (1-1=0). So the example will return the date of the last day of the current month.
you can use an assembly for doing this work by adding it as a reference.

Interval of one month back not working on the 31st?

Essentially, I have a query that is responsible for fetching all records (with specific filters) within the last month. I'm using Oracle's interval keyword and all was working great until today (December 31st, 2009). The code I'm using is
select (sysdate - interval '1' month) from dual
and the error I get it
ORA-01839: date not valid for month specified
How can I use the interval keyword to be compatible with any date? Or if anyone has a better way of approaching the issue, I'm all ears.
Thank you.
try
select add_months(sysdate,-1) from dual
Being pedantic...
The requirements are not quite specified perfectly unambiguously. What does the business mean by "within the last month"? Most people would take that to mean "within the current calendar month" in which case I'd use:
TRUNC(SYSDATE,'MM')
Otherwise, perhaps they want an arbitrary period of 1 month prior to the current date - but then how do you define that? As you've found, INTERVAL '1' MONTH simply subtracts one from the month portion of the date - e.g. 15-JAN-2009 - INTERVAL '1' MONTH returns 15-DEC-1999. For some dates, this results in an invalid date because not all months have the same number of days.
ADD_MONTHS resolves this by returning the last day in the month, e.g. ADD_MONTHS(31-DEC-2009,-1) returns 30-NOV-2009.
Another possibility is that the business actually wants to use an average month period - e.g. 365/12 which is approximately 30.4. They might want you to use SYSDATE-30, although of course twelve iterations of this will only cover 360 days of the year.

Resources