How to calculate end of week in SQL? - teradata

Given a date format yyyy-mm-dd, how would I calculate the saturday at the end of the week? For a sunday, I would like to calculate the saturday of the following week. I am using teradata 15.

When you run the query on Saturday do you want to get the same day or the next Saturday?
next_day(date-1, 'sat') -- same day
next_day(date, 'sat') -- next Saturday

Related

extract 2 days of previous data and exclude weekends in pl/sql

Looking assistance in PL/SQL query.
Business Case:
Extract 2 previous business days of data from DATE column excluding weekends.
(Business Days - Monday to Friday)
End users are going to provide only 1 date to extract data.
Example:
If Effective_date is on Monday, then need previous 2 working days data of Thursday and Friday.
If Effective_date is on Tuesday, then need previous 2 working days data of Friday and Monday.
If Effective_date is on Wednesday, then need previous 2 working days data of Monday and Tuesday. and so on.....

Yearweek is parsed wrongly in R

Problem: I am facing the problem that R parses a date (30 December 2019) into yearweek wrongly (Output: 2019 W01). I do not know why this is happening. Any suggestions what to change/alternative way of coding?
format(lubridate::ymd("2019-12-30"), "%Y W%V")
# Output
# 2019 W01
# Desired Output:
# 2019 W52
From the strptime documentation:
%U
Week of the year as decimal number (00–53) using Sunday as the first day 1 of the
week (and typically with the first Sunday of the year as day 1 of week 1). The US
convention.
%V
Week of the year as decimal number (01–53) as defined in ISO 8601. If the week
(starting on Monday) containing 1 January has four or more days in the new year,
then it is considered week 1. Otherwise, it is the last week of the previous year,
and the next week is week 1. (Accepted but ignored on input.)
%W
Week of the year as decimal number (00–53) using Monday as the first day of week
(and typically with the first Monday of the year as day 1 of week 1). The UK
convention.
It sounds like you may want either %U or %W, depending on whether you want to treat Sunday or Monday as the start of the week.
Note however that these can result in values between 00 and 53, which is a consequence of fixing the start of the week to a particular weekday (either Sunday or Monday). Doing that means that there can actually be a partial week at the start and at the end of the year.
If you prefer to count based on week number 1 beginning on the first day of the year, you can use the function lubridate::week.
For example:
library(lubridate)
year_week <- function(date) paste0(year(date), ' W', week(date))
year_week(ymd("2019-01-01"))
# Result: "2019 W1"
year_week(ymd("2019-12-30"))
# Result: "2019 W52"
After some more research I found that this is the best solution:
format(lubridate::ymd("2019-12-30"), "%G W%V")
Use %G instead of %Y to reflect that the week-based year (%G and %g) may differ from the calendar year (%Y and %y).
See also: https://community.rstudio.com/t/converting-week-number-and-year-into-date/27202/2

Can I define exact weekday in iso8601 datetime to schedule a job?

The date-time that i have now:
"schedule": "R/2017-10-05T17:21:00/PT15M"
for now the job is scheduled for every 15 minutes (in chron), but if i want to perform it three times a day at a certain time and only Monday - Friday?
Is it possible to define in this format?
It's not possible
ISO8601 is designed to define intervals but only static. There is no way to define weekdays or weekends. This means you can only define interval for 3 times a day but not every day from Monday to Friday because then you will have not equal intervals between Friday and Monday.
What you can do is to create 15 jobs scheduled weekly.

How to find a previous weekday (Sunday, Monday, etc.) from a specific date using Momentjs

I'm trying to figure out how to build code that will find the 'MM/DD/YYYY' date of a previous day of the week in relation to a given date, which is 04/10/18.
If I've understood Momentjs' documentation correctly, I think I can use negative weekday numbers to find a past 'X-day' from a specific date.
I know the date I'm using is a Tuesday, so I thought if I gave the weekday function the numerical representation of last Tuesday (-2), I would get the past Tuesday, '04/03/2018'. The problem is that when I run the following check:
expect(await moment('04/10/2018', ['MM/DD/YYYY']).weekday(-2).format('MM/DD/YYYY')).toBe('04/03/2018');
I get a result of '04/06/2018' instead.
The returned value is correct. The function .weekdays(-2) computes two weekdays earlier than the given moment.
In your example, two weekdays before a Tuesday would be a Friday, because one weekday before Tuesday is Monday, and one weekday before that is Friday, so Tuesday 4/10/2018 - 2 weekdays = Friday 4/06/2018.

SQLite return wrong week number for 2013?

I have a simple SQL for calculating week number in my reports on SQLite
SELECT STRFTIME('%W', 'date_column')
It was correct for 2009-2012. In 2013 I got always the wrong week number.
For example
SELECT STRFTIME('%W', '2012-02-28')
return '09' and this is correct.
SELECT STRFTIME('%W', '2013-02-28')
return '08' and this is wrong. We have the 9th week.
Is there something in SQLite date time functions that I don't understand? Or is it a bug of SQLite?
CL's answer works fine for OP's definition of "right", which is not quite the same as ISO definition. ISO week numbers are always in the range 1-53 (no week 0), and the last 3 days of a year may fall into Week 1 of the following year, just like the first 3 days may fall into Week 52 or 53 of the preceding year. To take these corner cases into account, you need to do something like:
SELECT
(strftime('%j', date(MyDate, '-3 days', 'weekday 4')) - 1) / 7 + 1 AS ISOWeekNumber
FROM MyTable;
As a side note, SQLite's Date and Time documentation does link to the POSIX strftime man page, which defines %W modifier as:
"week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0."
To convert from SQLite's undocumented week definition (first week is the week with the year's first Monday in it, or the week with 7 January in it) to the ISO week definition (first week is the week with the year's first Tuesday in it, or the week with 4 January in it), we let SQLite compute the week of the year's 4 January. If that is not one, we have to increase the week number:
SELECT strftime('%W', MyDate)
+ (1 - strftime('%W', strftime('%Y', MyDate) || '-01-04'))
FROM MyTable

Resources