Date formatting with IYYY vs YYYY:
SELECT to_char( '2012-12-31'::DATE, 'IYYY-MM-DD' ) AS weird,
to_char( '2012-12-31'::DATE, 'YYYY-MM-DD' ) AS expected;
Result:
weird | expected
------------+------------
2013-12-31 | 2012-12-31
It would not be this surprised if the "weird" result was "2013-01-01".
What's wrong here ? (PostgreSQL 9.1.7)
IYYY refers to the year of the week of the specified date.
The first week of 2013 starts on Monday, 31 December 2012, so the weird result in the question is correct.
There's a warning directly related to this in the manual:
Attempting to construct a date using a mixture of ISO week and
Gregorian date fields is nonsensical, and will cause an error. In the
context of an ISO year, the concept of a "month" or "day of month" has
no meaning. In the context of a Gregorian year, the ISO week has no
meaning. Users should avoid mixing Gregorian and ISO date
specifications.
See also wikipedia iso week date article for lots of details.
Related
I want to find out the previous weeks's last day in Teradata SQL using Sunday as the last day of the week. For example, today is Friday 1/27, so the last week ended on Sunday (1/22) and I would want to return 2023-01-22.
Other examples:
If current date is '2023-01-02', then the output I require is '2023-01-01'
If current date is '2023-01-18', then the output I require is '2023-01-15'
With Hive query I would use:
date_sub(current_date, cast(date_format(current_date, 'u') as int));
What would the equivalent be in Teradata? I've tried using the code below but it seems to return the date of the closest Sunday instead of the date of the previous Sunday.
SELECT ROUND(current_date, 'd') (FORMAT 'yyyy-mm-dd');
There are several ways:
Probably the best one is one of the built-in functions to return the previous xxxday <= the input date:
Td_Sunday(Current_Date - 1)
Or the function to return the next xxxday > input date:
Next_Day(Current_Date - 8, 'sun')
Truncating is least understandable:
Trunc(Current_Date, 'IW') -1
TRUNC supports three variations, only IW is usable, but restricted to Monday as week start:
IW: the Monday of the ISO week
WW: the same day of the week as January 1st of the year
W: the same day of the week as the first day of the month
You can use the trunc function to return the first day of the a week, month, ect.
select trunc(current_date -7 ,'IW')
Current date today is 2023-01-27. This will return 2023-01-15, the previous Sunday.
EDIT: Sorry, meant to use the ISO week. As Dnoeth points out, the regular week option doesn't work consistently (which I didn't know, never used it for this before). Anyhoo, his answer is better than mine...
I have some date time strings with day first. When I try to convert to TIMESTAMPTZ the month seems to be ignored.
Example
TO_TIMESTAMPTZ('01/07/2020 04:00', 'DD/MM/YYYY HH:MM')
Returns
2020-01-01T04:00:00.000+00:00
I'm looking for 1st July but get 1st January (not even 7th January!)
The pattern for minutes is MI, not MM (see documentation):
dbadmin=> select TO_TIMESTAMP_TZ('01/07/2020 04:00', 'DD/MM/YYYY HH:MI');
TO_TIMESTAMP_TZ
------------------------
2020-07-01 04:00:00+02
I am trying to convert a string to date in Redshift.
select to_date('Fri Apr 03 00:00:07 2020','Dy Mon DD hh24:mi:ss YYYY')
I am getting an issue Invalid operation: Invalid date format: Specified day twice.
Is it not possible to mention both Day name and Day of month as a number in the same date string?
I am following this reference for date formats in Redshift
So you are specifying the date twice in your format string - day of week and day of month. Which is Redshift to use if they are in conflict? The reference you provided is general in nature, specifying both input and output format patterns (converting to a string you may want both date and day of week). If you just want to ignore the day of the week in the input string just use the format string 'XXX Mon DD hh24:mi:ss YYYY'.
While answering Regexp matching ISO8601 date/time format I wondered how you express first coming Monday or repeat every monday in ISO8601.
My guess would be something like:
W-1
R/W-1
But I cannot find a confirmation of that.
ISO 8601 has the concept of week dates which allow you to specify a week of the year and the day of the year. In the following example 2014-W01-1 means the first week of 2014 on Monday. Then repeat that every week using P1W
R/2014-W01-1T00:00:00/P1W
Seen in this answer
Using TSQL I need to get the ISO Week Number in a Month for a give ISO Year Week Number.
For example: The following code will give me Week #1 for 12/31/2001, which is correct. It is the first Monday in 2002 and the first day of the ISO Year 2002.
select DATEPART(ISO_WEEK, '12-31-2001'); --Week 1 January 2002
My question is how do I...
(1) Take the ISO Week Number Example: ISO Year Week Number: 14 for April 4, 2016 (April Week #1).
(2) Now Take ISO Year Week Number 14 and return April Month Week Number = 1 for the example above.
There seems to be nothing in SQL Server to get the ISO Month Week# from the ISO Year Week Number. I have a function I wrote but it is has some hacks to get it to work, but not 100%.
I think you want something like this... but am not sure why you need the ISO_WEEK. Just replace getdate() with your column.
select datepart(wk, getdate()) - datepart(wk,dateadd(m, DATEDIFF(M, 0, getdate()), 0)) + 1
After attempting to handle getting ISO Month Week Number in functions I decided an easier solution was to create an ISO_Calendar table in SQL Sever.
We know in TSQL you can get the ISO Year Week Number and some a bit of work the ISO Year Number. The ISO Month Week Number is another story.
I build the table shown below with data from 2000 to 2040 by populating all the columns other than the ISO Month number. Then on a second pass I looped through the table and set the ISO Month number based on the Month# in the Monday Date.
Now if I want to get the ISO Month number for a date I check #Date between Monday and Sunday. In my case I am only concerned with dates between Monday and Friday since this is for a stock analysis site.
select #ISOMonthWeekNo = c.ISOMonthWeekNo
from ISO_Calendar c
where #TransDate between c.Monday and c.Sunday;
The advantage is the table is a one time build and easier to verify the accuracy of the data.