Yup minimum age validation with day month year values from dropdown - yup

Using select control to pickup day, month, year of dob
How to validate min age say 18 in yup itself
dateOfBirth: Yup.object().shape({
day: Yup.string().required('day is required'),
month: Yup.string().required('month is required'),
year: Yup.string().required('year is required')
})

Related

How to Average by day of week in Google Sheets?

Given a table of dates and values.
How to average by day of week?
I found AVERAGEIF to average and TEXT(E4, "dddd") to convert to day of week.
But how to combine those two functions?
Date
Value
1/1/2001
1
1/2/2001
2
1/3/2001
3
1/4/2001
3
1/5/2001
6
1/6/2001
3
Day of Week
Average
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Go with QUERY() function. Try-
=QUERY({INDEX(TEXT(A1:A15,"dddd")),B1:B15},
"select Col1, avg(Col2) group by Col1 label Col1 'Day', avg(Col2) 'Average'")
To make it dynamic, use-
=QUERY({INDEX(TEXT(TOCOL(A1:A,1),"dddd")),TOCOL(B1:B,1)},
"select Col1, avg(Col2) group by Col1 label Col1 'Day', avg(Col2) 'Average'")

Extract year from big date in postgreSQL ('date out of range for timestamp')

I get an error "date out of range for timestamp" in the following situation:
select '1000000-01-01'::date; --> 1000000-01-01 -> ok!
select extract(year from ('1000000-01-01'::date)) -> date out of range for timestamp
select to_char('1000000-01-01'::date, 'YYYY') -> date out of range for timestamp
I guess the problem is that somewhere the date is converted to timestamp.
How to extract the year in my situation?
Yeah, it seems any time you do an operation on a date it gets transformed to a timestamp. The best I could come up with:
select split_part('1000000-01-01', '-', 1) as year;
year
---------
1000000
--If value is actual date:
select split_part('1000000-01-01'::date::text, '-', 1) as year;
year
---------
1000000

convert numeric week of year to a date (yyyy-mm-dd) in hiveql

It is easy enough to extract a week value from a date in hiveql:
select date_format('2020-10-18','w');
Is there a function for reversing this process: extracting the end date when provided a year and week number?
To get accurate date you need to provide also week day along with year and week number in a year.
select date_format('2020-10-18','w'), from_unixtime(unix_timestamp('2020, 43, 7', 'yyyy, w, u'), 'yyyy-MM-dd');
Returns:
43 2020-10-18
It looks like week number in a year counted from Sundays and day number in a week is counted from Mondays because Monday is 19th:
select date_format('2020-10-18','w u'), from_unixtime(unix_timestamp('2020, 43, 1', 'yyyy, w, u'), 'yyyy-MM-dd');
returns
43 2020-10-19
If that is true, you can fix it by subtracting 60*60*24 from unix_timestamp:
select date_format('2020-10-18','w'), from_unixtime(unix_timestamp('2020, 43, 1', 'yyyy, w, u')-60*60*24, 'yyyy-MM-dd');
Returns:
43 2020-10-18
UPDATE: Surprisingly, if not providing day in a week, only year and week number, it works also counting Sunday as a week day by default but it will be not correct for other dates for example 2020-01-20, it will return the same Sunday 2020-01-18, check it yourself:
select date_format('2020-10-18','w'), from_unixtime(unix_timestamp('2020, 43', 'yyyy, w'), 'yyyy-MM-dd');
returns:
43 2020-10-18
So, if you do not have day in a week and do not need absolutely accurate date, then use
from_unixtime(unix_timestamp('2020, 43', 'yyyy, w'), 'yyyy-MM-dd');
Or like this (year and week number are selected from the table):
select from_unixtime(unix_timestamp(concat(col_year, ', ', col_week), 'yyyy, w'), 'yyyy-MM-dd') from your_table;

Postgresql date range query with year and week number

Hi I would like to store the number of working hours for a given week in a table with,
hours, year, week
so I can aggregate the hours for a week quickly, where week is the ISO week number.
I then want to do a date range filter query on this table, let's say
From 2018-12-24 to 2019-01-21 (year 2018 week 52 to 2019 week 4).
If the user pass the year and week, then I would need to do a range check on a compound index where you compare the year value first, and then the week number.
How should I structure the query and index to efficiently retrieve records with this range?
This is a basic attempt, given a start year and start week, and an end year and end week:
select year, week, hours
from
weekly hours
where((year = 2018 and week >= 52) OR year > 2018) AND
((year = 2019 and week <=3) OR year < 2019)
You can compare more than one column with the <= or >= operator:
select *
from weekly_hours
where (year, week) >= (2018,52)
and (year, week) <= (2019,3);
That query can make use of an index on both columns, e.g.
create index on weekly_hours (year, week);
You can calculate the week as an absolute number and use this for your range queries. An expression index can be used for the absolute week calculation.
create table weekly_hours (
year int,
week int,
hours int
);
insert into weekly_hours
(year, week, hours)
values
(2018, 52, 10),
(2019, 4, 9),
(2019, 10, 9);
-- expression index that generates the absolute week
create index weekly_hours_absweek_idx on weekly_hours((year * 53 + week));
-- range query
select year, week, hours
from weekly_hours
where
year * 53 + week >= 2018 * 53 + 52
and year * 53 + week <= 2019 * 53 + 4;

Add X days to a Received Date but Exclude Weekends/Holidays from a Date Table

I hope someone can help with a calculation that I am having trouble developing.
I am developing a report in a DB2 database that I need to add "X" number of days to a "RECEIVED" date/time when an order comes in between X and Y; but exclude Weekends and Holidays to add to the received date. I have created a [TBLCALENDAR] that lists the Weekends and Holidays (Example below); and from this, I want to ADD X number of days to a "DUEDATE"
tblCalendar]
DATE DAYOFWK DAY HOLIDAY
1/19/2019 7 Saturday
1/20/2019 1 Sunday
1/21/2019 2 Monday YES
So, for example 1, if I have an order that is placed on 1/18/2019 at 4:01pm; the due date should be 1/23/2019 at 11:00am.
Example 2: if I have an order that is placed on 1/18/2019 at
Conditions are:
Previous Date 4:01pm to Current Date 11:00am = Due Date should be + "X" business days by 11:00am
If order received Current day by 4:00pm = Due Date should be + "X" business days by 4:00pm
I have tried to reference the tblCalendar to get the [Received] date/time and add X number of days based off of an order, but it's not functioning the way I have hoped.
I have used the following code...but it doesn't exclude Weekends or Holidays when adding the specified number of days or have my order time requirement to take into account previous day after 4:00pm to current date of 11:00am:
RECEIVEDDATETIME + 2 days as DUEDATE;
I have also used the below code to reference TBLCALENDAR to find the # of holidays and weekend days in a date range:
( SELECT COUNT (*) FROM TBLCALENDAR AS C WHERE C.HOLIDAY = 'YES'
AND C.DATE BETWEEN TBLORDERS.RECEIVEDDATETIME
AND TBLORDERS.DUEDATETIME) +
(SELECT COUNT (*) FROM TBLCALENDAR
WHERE DAYOFWK IN (1,7)
AND DATE BETWEEN TBLORDERS.RECEIVEDDATETIME
AND TBLORDERS.UPLOADTIME) AS NONWORKINGDAYS
Expected field output
If order was received between 1/17/2019 4:01pm to 1/18/2019 10:59am = 1/23/2019 11:00am
If order received Current day by 4:00pm 1/18/2019 3:59am= 1/23/2019 by 4:00pm.
RECEIVEDDATETIME DUEDATE
1/17/2019 4:01pm 1/23/2019 11:00am
1/18/2019 10:00am 1/23/2019 4:00pm
Here is a solution without the time logic.
with tblCalendar(DATE, DAYOFWK, DAY, HOLIDAY) as (values
(date('2019-01-19'), 7, 'Saturday', '')
, (date('2019-01-20'), 1, 'Sunday', '')
, (date('2019-01-21'), 2, 'Monday', 'YES')
, (date('2019-01-22'), 3, 'Tuesday', '')
, (date('2019-01-23'), 4, 'Wednesday', 'YES')
, (date('2019-01-24'), 5, 'Thursday', '')
, (date('2019-01-25'), 6, 'Friday', '')
, (date('2019-01-26'), 7, 'Saturday', '')
)
, mytab (RECEIVEDDATE, DAYS2ADD) as (values
(date('2019-01-19'), 2)
, (date('2019-01-20'), 2)
, (date('2019-01-21'), 2)
, (date('2019-01-22'), 2)
)
select m.*, t.date as DUEDATE
--, dayofweek(date) as DAYOFWK, dayname(date) as DAY
from mytab m
, table
(
select date
from table
(
select
date
, sum(case when HOLIDAY='YES' or dayofweek(date) in (7,1) then 0 else 1 end) over (order by date) as dn_
from tblCalendar t
where t.date > m.RECEIVEDDATE
)
where dn_ = m.DAYS2ADD
fetch first 1 row only
) t;
The idea is to enumerate each day of the calendar after the RECEIVEDDATE (1-st parameter) starting from 1 with the following logic: the number of each day increases by 1 if it's non-holiday non-weekend day (the sum(...) over(...) expression).
Finally, we select a date with the corresponding number of days needed to add (2-nd parameter).
Solution idea:
Your tblCalendar is a good idea but I recommend to add the working day information instead of (only) flagging the holidays and weekends. The problem with the "off days" are that after you have figured out how many of them are in the period from your receive date to the receive date + X days you cannot easily add them because there could be other "off dates" in that perios again.
By numbering all the work days you could identify the workday which is closest (equal or bigger) to the receive date. Retrieve its number and add the X days to that number. Retrieve the date that has this work day number and you are fine.
The time logic should be built before that all because it could add another day to the X days.