adding days to date in apex - sql

I'm making a form where the user inputs their information and chooses a reservation date. So, the user picks a reservation date, and based on that date, I want to generate an end date that is 4 days after the start date.
I also want to make it so that if their reservation starts on a Monday it ends on a Thursday or if it starts on a Thursday it ends on a Monday.
How can I get the end date?

You will have to create your own PL/SQL stored procedure which will take in parameters the start date and the number of days to add.
Then your procedure will add the number of days and you will have a temp endDate.
Then, you will only have to check the day of the temp endDate, and if it is a weekend day, return the next Monday.
Apex allows you to have a dynamic action which can call a stored procedure and then update an item of your page.
Hope this helps.

Related

How to identify the next working day using SQL code?

If I have sunday as non working day and Monday as Public holiday how to exclude these days while calculating the time difference between the record created date and the time when the record was called for the first time. Creation_time, Attempt_time are attributes available.
The data for holidays list is loaded in a separate table.

Amazon Redshift- How to get start date of the Week from existing daily date field from the table?

I am trying to get start date of the week from existing daily date field from the same table. For example daily dates from 05/08/2022 to 05/14/2022 , the start of the week date output need to come as 05/08/2022 for all days in the week. week start on Sunday.
Also similar thing require to first date of the Month and quarter(3 month division)
The date_trunc() function performs this operation - https://docs.aws.amazon.com/redshift/latest/dg/r_DATE_TRUNC.html

how to automate the date change in a query using transact sql

I work for a company where everyday I modify a query by changing the date of the day before, because the report is always from the previous day.
I want to automate the date change. I have made a table with two columns, one with all dates from this year and another with bits where if 0 is a working day and 1 if is a holiday.
I have successfully automated a little bit by telling if the day before is a working day then subtract 1 from the date (This is what happens everyday). But the problem is, that if is Monday appears as Friday, because Saturday and Sunday are not billable. And let's also say, that if today is Thursday and Wednesday and Tuesday we're holidays, then the report will run on Monday. I will leave you a picture, that shows how the table is made with dates.
Remembering, that if there is no holidays in the middle of the week, always will be subtract one.
The way to do this sort of thing is close to what you have done, but just extend it further. Create a BusinessDate table that has every date, and then every rule you have implemented inside it. You can go so far as to include a column such as ReportDate which will return,for every date, what date the report should be run for.
Do this once, and it will work forever more. You may have to update for future holidays once a year, but better than once a day!
It will also allow you to update things specific for your business, like quarter dates, company holidays, etc.
If you want to know more on the subject, look up topics around creating a date dimension in a data warehouse. Its the same general issue you are facing.
Too complicated for a comment and it involves a lot of guessing.
So everyday, your process starts by first determining if "today" is a work day. So you would do something like:
if exists (select * from <calendar> where date = cast (getdate() as date) and IsWorkday = 1")
begin
<do stuff>
end;
The "do stuff" section would then run a report or your query (or something that isn't very clear) using the most recent work day prior to the current date. You find that date using something like:
declare #targetdate date;
set #targetdate = (select max(date) from <calendar>
where date < cast (getdate() as date)
and IsWorkday = 1);
if #targetdate is not null
<run your query using #targetdate>
That can be condensed into less code but it is easier to understand when the logic is written step-by-step.

How to insert a record on next selected date if count of a date is full

I have a table where I maintain working days in a week like 2nd and 4th day and number of records it can accept is 10 records per working day
usercode DaysofWeek NumberOfRecords
0623PO54 2 10
0623PO54 4 10
On insertion I have application date example 01-09-2017(dd/mm/yyyy) which is Friday.
Now I have to insert this record in closest working day from 01-09-2017 which is 05-09-2017 as it is 2nd working day. After inserting 10 records next records should be insert on 4th working day which is 07-09-2017.
I don't know how to get closest date from application date and insert record on it.
If you also want to exclude holidays than use a master table for Ex. CalenderMastr of dates which have holidays flag and day of week like Monday=2. and as you mansion that you are maintaining working days in a table for ex. workdaymaster. Now make a select query to get next date from CalenderMastr from current date and day of week stored in workdaymaster. now on output date check if in your transaction table count is smaller or not if count is small insert new record or if not than move to next date using while loop in your query. hope you can understand what i am trying to say.

How to group by week specifying end of week day?

I need to run a report grouped by week. This could be done by using group by week(date) but the client wants to set the day of the week that marks the end of week. So it can be Tuesday, Wednesday etc. How can I work this into a group by query?
The datetime column type is unix timestamp.
The WEEK() function takes an optional second parameter to specify the start of the week:
This function returns the week number for date. The two-argument form of WEEK() enables you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53.
However, it can only be set to Sunday or Monday.
UPDATE: Further to the comments below, you may want to consider adding a new column to your table to act as a grouping field, based on WEEK(DATE_ADD(date INTERVAL x DAY)), as suggested in the comments. You may want to create triggers to automatically generate this values whenever the date field is updated, and when new rows are inserted. You would then be able to create a usable index on this new field as required.