and date(p.date_of_sale) <= current_date
I try this code and I got an answer like that.
.
But an error is shown like that.
.
Please watch 2 screenshots because I am not fluent in English and I don't know how to explain with text. That's why I add 2 screenshots.
Your problem comes from to_char(p.date_of_sale, 'yyyy "-week" iw') where
iw = week number of ISO 8601 week-numbering year (01–53; the first Thursday of the year is in week 1)
whereas yyyy = the year on 4 digits (not the ISO8601 year)
These two parameters are sometimes not consistent for instance for 2023 January the 1st :
SELECT to_char('20230101' :: date, 'yyyy') => 2023
SELECT to_char('20230101' :: date, 'iw') => 52
If you want to be consistent, you can either :
select to_char('20230101' :: date, 'YYYY"-week" w') => 2023-week 1
or
select to_char('20230101' :: date, 'IYYY"-week" iw') => 2022-week 52 (ISO8601 year and week)
see dbfiddle
Related
i am using the following code
DateFormat.yMMMMEEEEd("en_US").format((myDateTime)
that give me the following output (Date of writing my question)
Tuesday June 21 , 2022
well , now i don't want the previous format look like the previous output UNLESS if there is one week back as a maximum Period
for example i am looking for Format to Auto describe days of the week by (names) like following.
Assuming today is Tuesday
output => 'now' (if 5 mins as maximum ) else output => 'Today'
else:
output => 'yesterday' (if there one day back )
output => 'Sunday'
output => 'Saturday'
output => 'Friday'
output => 'Thursday'
output => 'Wednesday'
NOW finally we arrive to same day whish is Tuesday that means 1 week passed so i need output like my first format in my question
output => 'Tuesday June 21 , 2022'
And so
output => 'Tuesday June 21 , 2022'
output => 'Monday June 20 , 2022'
output => 'Sunday June 19 , 2022' ...... and so
in other words i only care of showing a week back in
their names only : now(if 5 mins as maximum) till A week back in
their names else DateFormat.yMMMMEEEEd("en_US").format((myDateTime)
what is the easiest and best way to do it . thanks
How can I get the number of days passed in the current quarter?
For example, if today is 1/2/2021, it will return 2.
If today is 2/5, it will return 36.
If today is 4/2, it will return 2.
Use date_trunc() to get the start of the quarter and subtract dates:
WITH cte(day) AS (
VALUES
(date '2021-01-02')
, (date '2021-02-05')
, (date '2021-04-02')
)
SELECT day
, day - date_trunc('quarter', day)::date + 1 AS days_passed_in_quarter
FROM cte;
day | days_passed_in_quarter
------------+------------------------
2021-01-02 | 2
2021-02-05 | 36
2021-04-02 | 2
+ 1 to fix off-by-one error as you clearly want to include the current day as "passed".
Always use unambiguous ISO 8601 date format (YYYY-MM-DD - 2021-02-05), which is the default in Postgres and always unambiguous, or you depend on the current datestyle setting (and may be in for surprises). Also avoids misunderstandings general communication.
Related:
PostgreSQL: between with datetime
I'm trying to find out the weekday i.e Mon, Tue, Wed etc. from a date-range formatted as yyyy mm dd
I tried to use the formula format(day(Date Table),"ddd"), but the weekday is wrong. In my example, the output of 2020.01.01 gives Sunday, but it should be Wednesday.
I think your formula is wrong:
Instead of
format(day(Date Table),"ddd")
Use
format(<Target Table>[<date column>],"ddd")
I.e. Omit the DAX DAY call. This is resulting in the day of the month (1..31) being passed to the format function.
When you use the DAY function in DAX, it returns the day of the month (1 through 31).
Thus DAY ( DATE ( 2020, 1, 1) ) = 1 which means you're trying to format the number 1 as a date. Integers are interpreted as days since 1899/12/30 when treated as a date, so 1 corresponds to 1899/12/31, which happened to be a Sunday. Thus FORMAT(1, "ddd") = "Sun".
There's no reason to get DAY involved here. You can simply write
Day = FORMAT ( 'Calendar'[Date], "ddd" )
I'm trying to manipulate a date value to go back in time exactly 1 ISO-8601 year.
The following does not work, but best describes what I want to accomplish:
date_add(date '2018-01-03', interval -1 isoyear)
I tried string conversion as an intermediate step, but that doesn't work either:
select parse_date('%G%V%u',safe_cast(safe_cast(format_date('%G%V%u',date '2018-01-03') as int64)-1000 as string))
The error provided for the last one is "Failed to parse input string "2017013"". I don't understand why, this should always resolve to a unique date value.
Is there another way in which I can subtract an ISO year from a date?
This gives the corresponding day of the previous ISO year by subtracting the appropriate number of weeks from the date. I based the calculation on the description of weeks per year from the Wikipedia page:
CREATE TEMP FUNCTION IsLongYear(d DATE) AS (
-- Year starting on Thursday
EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 5 OR
-- Leap year starting on Wednesday
(EXTRACT(DAY FROM DATE_ADD(DATE(EXTRACT(YEAR FROM d), 2, 28), INTERVAL 1 DAY)) = 29
AND EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 4)
);
CREATE TEMP FUNCTION PreviousIsoYear(d DATE) AS (
DATE_SUB(d, INTERVAL IF(IsLongYear(d), 53, 52) WEEK)
);
SELECT PreviousIsoYear('2018-01-03');
This returns 2017-01-04, which is the third day of the 2017 ISO year. 2018-01-03 is the third day of the 2018 ISO year.
Besides writing a custom UDF to support this issue, is there any known methods of achieving this? I'm currently using Hive 0.13.
Starting with Hive 1.2, you can also do it like this:
select next_day(date_sub('2019-01-01', 7), 'MON')
Output:
2018-12-31
date_sub(m.invitationdate,pmod(datediff(m.invitationdate,'1900-01-07'),7))
This expression gives the exact solution to my question.
Regards,
Boris
This is the easiest and the best solution for fetching 1st day of the week's date:
For Current timstamp:
select date_sub(from_unixtime(unix_timestamp()), cast(from_unixtime(unix_timestamp(), 'u') AS int)) ;
For any given date or column:
select date_sub(from_unixtime(unix_timestamp('2017-05-15','yyyy-MM-dd')), cast(from_unixtime(unix_timestamp('2017-05-15','yyyy-MM-dd'), 'u') AS int)) ;
select date_sub(from_unixtime(unix_timestamp(colname,'yyyy-MM-dd')), cast(from_unixtime(unix_timestamp(colname,'yyyy-MM-dd'), 'u') AS int)) ;
Yes, you can do this without writing a UDF. If you look at at the Hive documentation under datetime functions, there is a function from_unixtime() that takes a unix timestamp and a string pattern. A couple of functions down on the documentation page, there is a link that explains the different patterns you can use in this function. So, from your timestamp, you can extract the day of the week and proceed accordingly.
Example Data:
1445313193
1445313100
1445313146
1445040000
1445040023
1445040111
The first three are Monday, 2015-10-19 and the last three are Friday, 2015-10-16.
Query:
select day_of_week
, date_var
, case when day_of_week = 'Sun' then date_var
when day_of_week = 'Sat' then date_sub(date_var, 6)
when day_of_week = 'Fri' then date_sub(date_var, 5)
when day_of_week = 'Thu' then date_sub(date_var, 4)
when day_of_week = 'Wed' then date_sub(date_var, 3)
when day_of_week = 'Tue' then date_sub(date_var, 2)
when day_of_week = 'Mon' then date_sub(date_var, 1)
else NULL
end as first_day_of_week_date
from (
select from_unixtime(timestamp, 'EEE') day_of_week
, from_unixtime(timestamp, 'yyyy-MM-dd') date_var
from db.table ) A
Output:
Mon 2015-10-19 2015-10-18
Mon 2015-10-19 2015-10-18
Mon 2015-10-19 2015-10-18
Fri 2015-10-16 2015-10-11
Fri 2015-10-16 2015-10-11
Fri 2015-10-16 2015-10-11
So, for today it returns yesterday, which was Sunday, and for last Friday, it returns the previous Sunday, the 11th. I am making the assumption that by "first day of a week", you mean Sunday; if not, you can adjust the code to mean Monday. Hope this helps.