Why is last day of this year week 1 in oracle? - oracle11g

Why is this showing week 1? Although it's last day of year 2012.
to_char( to_date('31-DEC-12'), 'IW' )

Because, according to the ISO standard, we are in week 1.
http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2013
The date format IW in Oracle returns the
Week of year (1-52 or 1-53) based on the ISO standard.
http://www.techonthenet.com/oracle/functions/to_date.php
For a non-ISO based year, you can try using the format WW.

Related

R not recognizing week number correctly [duplicate]

String contains 'YEAR WEEK' and I want to transform it with parse_date_time() to a date object but I can't make the code work:
parse_date_time(c("201510"), "YW")
I don't have to use lubridate, can be other packages, too.
Before converting year-week to a date you have to specify a day of the week but more importantly you have to ensure which of the different conventions is being used.
Base R's strptime() function knows 3 definitions of week of the year (but supports only 2 of them on input) and 2 definitions of weekday number,
see ?strptime:
Week of the year
US convention: 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): %U
UK convention: 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): %W
ISO 8601 definition: 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: %V which is accepted but ignored on input.
Note that there is also a week-based year (%G and %g) which is to be used with %V as it may differ from the calendar year (%Y and %y).
Numeric weekday
Weekday as a decimal number (1–7, Monday is 1): %u
Weekday as decimal number (0–6, Sunday is 0): %w
Interestingly, there is no format for the case Sunday is counted as day 1 of the week.
Converting year-week-day with the different conventions
If we append day 1 to the string and use the different formats we do get
as.Date("2015101", "%Y%U%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%U%w")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%w")
# [1] "2015-03-09"
as.Date("2015101", "%G%V%u")
# [1] NA
For weekday formats %u and %w we do get the same result because day 1 is Monday in both conventions (but watch out when dealing with Sundays).
For 2015, the US and the UK definition for week of the year coincide but this is not true for all years, e.g., not for 2001, 2007, and 2018:
as.Date("2018101", "%Y%U%u")
#[1] "2018-03-12"
as.Date("2018101", "%Y%W%u")
#[1] "2018-03-05"
The ISO 8601 format specifiers aren't supported on input. Therefore, I had created the ISOweek package some years ago:
ISOweek::ISOweek2date("2015-W10-1")
#[1] "2015-03-02"
Edit: Using Thursday to associate a week with a month
As mentioned above you need to specify a day of the week to get a full calendar date. This is also required if the dates need to be aggregated by month later on.
If no weekday is specified and if the dates are supposed to be aggregated by month later on, you may take the Thursday of each week as reference day (following a suggestion by djhurio). This ensures that the whole week is assigned to the month to which the majority of the days of the week belong to.
For example, taking Sunday as reference day would return
ISOweek::ISOweek2date("2015-W09-7")
[1] "2015-03-01"
which consequently would associate the whole week to the month of March although only one day of the week belongs to March while the other 6 days belong to February. Taking Thursday as reference day will return a date in February:
ISOweek::ISOweek2date("2015-W09-4")
[1] "2015-02-26"
Yes ISOweek package does this
ISOweek::ISOweek2date(isoWeek)
but for the other direction, check up the newer lubridate package as well
ISOweek::date2ISOweek(yourDate)
lubridate::isoweek(ymd(yourDate))

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

Transform year/week to date object

String contains 'YEAR WEEK' and I want to transform it with parse_date_time() to a date object but I can't make the code work:
parse_date_time(c("201510"), "YW")
I don't have to use lubridate, can be other packages, too.
Before converting year-week to a date you have to specify a day of the week but more importantly you have to ensure which of the different conventions is being used.
Base R's strptime() function knows 3 definitions of week of the year (but supports only 2 of them on input) and 2 definitions of weekday number,
see ?strptime:
Week of the year
US convention: 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): %U
UK convention: 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): %W
ISO 8601 definition: 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: %V which is accepted but ignored on input.
Note that there is also a week-based year (%G and %g) which is to be used with %V as it may differ from the calendar year (%Y and %y).
Numeric weekday
Weekday as a decimal number (1–7, Monday is 1): %u
Weekday as decimal number (0–6, Sunday is 0): %w
Interestingly, there is no format for the case Sunday is counted as day 1 of the week.
Converting year-week-day with the different conventions
If we append day 1 to the string and use the different formats we do get
as.Date("2015101", "%Y%U%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%U%w")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%w")
# [1] "2015-03-09"
as.Date("2015101", "%G%V%u")
# [1] NA
For weekday formats %u and %w we do get the same result because day 1 is Monday in both conventions (but watch out when dealing with Sundays).
For 2015, the US and the UK definition for week of the year coincide but this is not true for all years, e.g., not for 2001, 2007, and 2018:
as.Date("2018101", "%Y%U%u")
#[1] "2018-03-12"
as.Date("2018101", "%Y%W%u")
#[1] "2018-03-05"
The ISO 8601 format specifiers aren't supported on input. Therefore, I had created the ISOweek package some years ago:
ISOweek::ISOweek2date("2015-W10-1")
#[1] "2015-03-02"
Edit: Using Thursday to associate a week with a month
As mentioned above you need to specify a day of the week to get a full calendar date. This is also required if the dates need to be aggregated by month later on.
If no weekday is specified and if the dates are supposed to be aggregated by month later on, you may take the Thursday of each week as reference day (following a suggestion by djhurio). This ensures that the whole week is assigned to the month to which the majority of the days of the week belong to.
For example, taking Sunday as reference day would return
ISOweek::ISOweek2date("2015-W09-7")
[1] "2015-03-01"
which consequently would associate the whole week to the month of March although only one day of the week belongs to March while the other 6 days belong to February. Taking Thursday as reference day will return a date in February:
ISOweek::ISOweek2date("2015-W09-4")
[1] "2015-02-26"
Yes ISOweek package does this
ISOweek::ISOweek2date(isoWeek)
but for the other direction, check up the newer lubridate package as well
ISOweek::date2ISOweek(yourDate)
lubridate::isoweek(ymd(yourDate))

show only weekday from date

I'm trying to use a date value I have in my table to show the start date of a holiday as the day value (Monday, Tuesday etc) how would be the best way to go about getting the start day value in this format in VB.NET?
I'm currently working with the date format dd/mm/yy.
Use Weekday and WeekdayName functions
WeekdayName(Weekday(startDate))
Weekday , which returns a number that indicates the day of the week of a particular date. It considers the ordinal value of the first day of the week to be one.
WeekdayName , which returns the name of the week in the current culture that corresponds to a particular weekday number.

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