Access expression, add one year, minus a day - ms-access

I have a table in access, which has a contract start date, contract end date.
It also needs auto filled columns for the start and end date of each year between the contract start and end dates.
E.g, customer takes out 5 year contract.
01/01/2020 - 01/01/2025
Year 1’s end date would be 1 year, minus a day from the contract start date:
31/12/2020.
As mentioned, this needs to be written as an expression in Microsoft Access.
I can’t find info on multiple date calculations from a singular start date (DateAdd etc).
Thanks for any help

Use DateAdd in code (or query):
UltimoYear = DateAdd("d", -1, DateAdd("yyyy", 5, DateStart))
Replace 5 with 1 for one year.

Related

Is there any function in sql to extract week?

I want to extract week from datetime, the output I want is 'YY/week', where week is the week of the year (eg '201724' is the 24th week in 2017).
The term "week of the year" is too ambiguous.
The week may start from Sunday, Monday or another weekday
The weeks enumeration in the year may start from 0 or 1
The weeks enumeration in the year may start from the week which includes January, 1 (and hence may be partial) or from first complete week of the year
The last week of the year, if it is partial, may be counted or not
Each DBMS has its own functions (sometimes original, always with original names) that can return the number of the week in the year on a given date. But they can not always take into account the above features.
Important addition provided by jarlh:
ISO 8601 (#4.3.4):
The first calendar week of a year is the one that includes the first Thursday of that year.
The last calendar week of a calendar year is the week immediately preceding the first calendar week of the next calendar year.
Week 1 is the first week of a year.
A calendar week starts on a Monday.
ISO 9075 doesn't even mention weeks.
SELECT TO_CHAR(TO_DATE('19-FEB-22') , 'IW') from DUAL;
To get the corresponding four-digit year, use
SELECT TO_CHAR(TO_DATE('19-FEB-22'), 'IYYY') FROM DUAL;
TO_CHAR() having so many options like this read more in Oracle manual or extract portation of date Extract Portion of Date Time Value
OutPut
You can use the following Mysql type query to extract.
SELECT DATE_FORMAT(BirthDate, " %u %Y") FROM Employees;
where the BirthDate date column in the database and the Employees is the table name.
This will result
49 1968
08 1952
35 1963
Week and the year.
in postgresql:
SELECT to_char('2016-12-31 13:30:15'::timestamp without time zone, 'yy/ww') ;
result:
16/53

Calculate next wednesday based on any given date using ssis derived column expression

I am creating a package which will store a report's generation date. on basis of that date need to derive the next Wwednesday date.
Ex: report date is 11/11/2019 so wed date should be 13/11/2019.
This Derived Column Expression should provide the next Wednesday's date.
DATEADD("DAY",((1 + DATEDIFF("DAY",(DT_DATE)"1/1/1970",GETDATE())) / 7) * 7 + 6,(DT_DATE)"1/1/1970")
Parts (inside out).
Days since 1/1/1970 + 1.
(Divide by 7) (Implicit Cast to Int) (Multiple by 7) will round it to last Thursday.
When added days back to 1/1/1970 (+6 days more) will move to next occurring Wednesday.
Notes
Without the initial 1 day offset, If GETDATE() is a Wednesday, the result will be GETDATE()'s date.
GETDATE() can be swapped out with GETUTCDATE() or a date Variable as needed.
The +6 can be moved back to +5 if a Tuesday is desired.

Calculate start and end date for the given week number in particular month in angular

I want to calculate start date and end date for the particular given week in the month , (assuming my week starts on monday and ends on sunday).
So, What I want is if I select any week number lets say 1,2 ,3 or 4 I should be able to calculate the start date for that particular week and end date .
In addition to the week number for that particular month , I will also give an input of year and month.
selection of year month and week number will be from HTML page through a drop down select option.
And I am looking for some efficient way of doing this .
Use Moment.js
Get the value of week number
var weeknumber = moment("07-27-2017", "MM-DD-YYYY").week(); console.log(weeknumber);

SELECT where date is within X months away regardless of stored year

I currently have a query that is getting the records where their deadline is less than 3 months away. The deadline is stored as a Date, but the year is not important as this record should flag up every year.
My query:
SELECT client_name
FROM client
WHERE MOD(DAYOFYEAR(deadline) - DAYOFYEAR(CURDATE()), +365) <= 90
AND DAYOFYEAR(deadline) > DAYOFYEAR(CURDATE())
Apologies if there’s errors in the syntax as I’m writing this from memory – but it does work.
It works up until a deadline is in the first quarter and the current date is in the final quarter then it no longer returns the record. How do I get around this?
So the query needs to return the records that have a deadline within 3 months of the current date. The Year in the deadline date should be ignored as this could be years ago, but it is the day and month of the deadline that is important.
Or is the problem the date I am storing? Should I update this each year?
Thanks
One approach is to use a conditional test like this:
WHERE CONCAT(YEAR(NOW()),DATE_FORMAT(d.deadline,'-%m-%d'))
+ INTERVAL CONCAT(YEAR(NOW()),DATE_FORMAT(d.deadline,'-%m-%d'))<NOW() YEAR
< NOW() + INTERVAL 3 MONTH
We can unpack that a little bit. On that first line, we're creating a "next due" deadline date, by taking the current year, and appending the month and day value from the deadline.
But there's a problem. Some of those "next due" deadline dates are in the past. So, to handle that problem (when the 3 month period "wraps" into the next year), we need to add a year to any "next due" deadline date that's before the current date.
Now, we can compare that to a date 3 months from now, to determine if the "next due" deadline date is in the next 3 months.
That's a bit complicated.
Here's a SQL Fiddle as a demonstration: http://sqlfiddle.com/#!2/c90e9/3.
For testing, NOW() is inconvenient because it always returns today's date. So, for testing, we replace all occurrences of NOW() with a user-defined variable #now, and set that to various dates, so we can appropriately test.
Here's the SQL statement I used for testing. The first expression is the conditional test we're planning on using in the WHERE clause. For testing, we want to return all the rows, and just see which rows get due_in_3mo flagged as TRUE (1) and which get flagged as FALSE (0).
The second expression in the SELECT list just the "next due" deadline date, same as used in the first expression.
The rest of the expressions are pretty self-explanatory... we also want to display the date 3 months in the future we're comparing to, and the original "deadline" date value.
SELECT
CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))
+ INTERVAL CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))<#now YEAR
< #now + INTERVAL 3 MONTH
AS `due_in_3mo`
, CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))
+ INTERVAL CONCAT(YEAR(#now),DATE_FORMAT(d.deadline,'-%m-%d'))<#now YEAR
AS `next_due`
, d.id
, d.deadline + INTERVAL 0 DAY AS `deadline`
, #now + INTERVAL 3 MONTH AS `now+3mo`
, #now + INTERVAL 0 DAY AS `now`
FROM d d
CROSS
JOIN (SELECT #now := '2015-11-01') i
ORDER BY d.id
Change the value assigned to #now in the inline view (aliased as i) to test with other date values.
(You may want to use DATE(NOW()) in place of NOW() so that times don't get mixed in, and you may want to subtract another day from that, that really just depends how you want to handle the edge case of a deadline with month and day the same as the current date. (i.e. do you want to handle that as "in the past" or not.)
To summarize the approach: generate the "next due" deadline date as a DATE value in the future, and compare to the a date 3 months from now.

Determine if date is before or after the 15th of the month

I have a function which applies a rate based on the number of months that an item has been stored. However we charge half that rate if the item was stored after the 15th. The function I'm using to determine the amount of months an item is stored is the DateDiff() Function. It returns the number of months between two dates. How do I determine if the date is before the 15th or after? If I use DateDiff(d,1/1/2015, 4/1/15) I would get a value greater than 15 so I cannot see if the Date is before or after the 15th using greater than or less than 15. How can I determine if either of the dates (entrance date which is the date the item entered into the storage area and the exit date which is the date it is removed) are before or after the 15th?
VBA models a date as an floating point double. The integral part stands for the day. It advances by 1 per day.
It provides a function Day(date) which you can use to extract the day of the month in which date occurs.
For example, CDate(40000) will return you 6-July-2009, and Day(40000) returns you 6.
I would do something like
if format(date, "DD") <= 15 then
msgbox "this is 15 or below"
else
msgbox "this is 16 and above"
end if
You can put a date variable in instead of date which is a built in function to return the system date.