IIf Statement Looking at Months - ms-access-2016

I would like to create an IIf statement to calculate when the expected leaving date of a child is.
For example, a child who is born before 31/08 is expected to leave nursery after 4 years, a child who is born after that date is expected to leave after 5 years.
Now what I was trying to do is ask an IIF statement which looks at the date of birth and decides whether to calculate for 4 years or for 5 years. However I keep running into issues with the code that I am using which is
= IIf([Date of Birth]>#31/08/0000# , =DateAdd("yyyy",4,[Date of Birth]) , =DateAdd("yyyy",5,[Date of Birth]))
as there are multiple children with different dates of birth. There needs to be a way to look specifically at the months only.
EDIT:
Turns out that is not what my boss needs, what he needs is basically to display when the child is leaving from the nursery i.e. when the new school term rolls around and the child is 4 years old. if the child is born before September he is applicable to start school that year. if he isn't the child is applicable to start school the next year on the month of September.
And right now I have no idea what to do as my attempts of doing an IIF function have completely failed. Can anyone Help?

Try with:
=DateAdd("yyyy", IIf([Date of Birth] > DateSerial(Year([Date of Birth]), 8, 31), 4, 5), [Date of Birth])
Edit 1:
You can use DateAdd like:
=IIf(DateAdd("yyyy", 4, [Date of Birth]) < DateSerial(Year(Date()), 9, 1), "Start school this year", "Postpone school start")
Edit 2:
Or you could calculate the age of the children on September 1st:
AgeAtSeptember: Age([Date of Birth], DateSerial(Year(Date()), 9, 1))
using this function:
' Returns the difference in full years from DateOfBirth to current date,
' optionally to another date.
' Returns zero if AnotherDate is earlier than DateOfBirth.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
' any date/time value of data type Date
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28th when adding a count of years to dates of Feb. 29th
' when the resulting year is a common year.
'
' 2015-11-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Age( _
ByVal DateOfBirth As Date, _
Optional ByVal AnotherDate As Variant) _
As Integer
Dim ThisDate As Date
Dim Years As Integer
If IsDateExt(AnotherDate) Then
ThisDate = CDate(AnotherDate)
Else
ThisDate = Date
End If
' Find difference in calendar years.
Years = DateDiff("yyyy", DateOfBirth, ThisDate)
If Years > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of DateOfBirth.
If DateDiff("d", ThisDate, DateAdd(IntervalSetting(DtInterval.dtYear), Years, DateOfBirth)) > 0 Then
Years = Years - 1
End If
ElseIf Years < 0 Then
Years = 0
End If
Age = Years
End Function

Related

Best practice to create a node with date for every day in one year with neo4j?

I need a node for every day with date for a certain year.
Is there a library that i can use for this? I use the neo4j-community version. To create every node manualy cant be the only solution?
You can try this:
with 2019 as pYear
with date({year: pYear - 1, month: 12, day: 31}) as startDate, date({year: pYear, month: 12, day: 31}) as endDate
with startDate, duration.indays(startDate, endDate) as daysInYear
with startDate, range(1, daysInYear.days) as daysList
foreach( dayNum in daysList |
create (:DateNode {date: startDate + duration({days: dayNum})})
)
Line 1 sets a local name for the year you want to generate the calenadar for
Line 2 sets local names for the last day of the previous year and the last day of the year of interest
Line 3 then works out how many days there are in the year as a duration. (This is leap year safe.)
Line 4 then creates an integer list of the offset in days from the end of the previous year for any date in the year of interest
The foreach in lines 5 to 7 then just iterates over the list and creates a node with property 'date' for each day of the year. The date is created as a temporal type.
You can discard a lot of the 'with'ery in the query, it's just there to show how the query is developed.
You can set up a multi year calendar by setting an arbitary year for the year in endDate

How to accurately calculate person's age in a perform screen

Informix-SE 4.10 with isql 4.10 DD6:
I have a DATE column, BirthDate, linked to a perform field tag "dob". When users enter the dob, I am doing the following date arithmetic:
age = (TODAY - dob ) / 365.25
field tag age is DISPLAYONLY TYPE DECIMAL field, with FORMAT="##.##".
This method is not producing accurate results.
Business Rule: Example, If a person true age is not 18 years old the day before their 18th birthday, I need to display 17 in the age field tag, and abort insertion of the customer row. Person must be >=18 years old.
DATETIME and INTERVAL datatypes are supported in this ifx version, but users are only comfortable entering dates in MMDDYYYY format.
In order to determine if the birthday has happened this year, you must be sure not to generate a non-valid date in the case where dob is February 29 and TODAY is in a non leap-year. Since 2000 is a leap-year, you can avoid the problem making the subtraction always for the year 2000.
MDY(MONTH(dob), DAY(dob), 2000) > MDY(MONTH(TODAY), DAY(TODAY), 2000)
Finally, subtract one from the year difference if so:
age = (YEAR(TODAY) - YEAR(dob) -
CASE WHEN MDY(MONTH(dob), DAY(dob), 2000) > MDY(MONTH(TODAY), DAY(TODAY), 2000)
THEN 1 ELSE 0 END)::int

Informix - Need to create date time parameters for Where clause

Informix is not my normal environment and the way it handles datetime values is throwing me for a loop. I can't imagine this is difficult, but for the life of me I'm not yet able to figure it out.
This is the SQL:
SELECT agentid,
extension As Ext,
resourcefirstname As FirstNm,
resourcelastname As LastNm,
Min(eventdatetime) As FirstIn
FROM agentstatedetail AS asdr Join
resource As r On asdr.agentid = r.resourceid
WHERE asdr.eventdatetime BETWEEN '2016-10-20 04:00:00' AND '2016-10-21 03:59:59'
AND eventtype = 3
AND assignedteamid = 14
Group By agentid, extension, resourcefirstname, resourcelastname
Order By Min(eventdatetime)
Everything works as is, but the dates in the Between clause are currently entered manually- not optimal. I just need some way to describe "yesterday at 4:00 AM" and "Today at 4:00 AM" Will somebody please clue me in?
Using Informix version 12.10.FC6DE, I can do this:
SELECT
TODAY::DATETIME YEAR TO SECOND AS today_zerohour
, TODAY::DATETIME YEAR TO SECOND - '20:00:00'::INTERVAL HOUR TO SECOND AS yesterday_dawn
, TODAY::DATETIME YEAR TO SECOND + '04:00:00'::INTERVAL HOUR TO SECOND AS today_dawn
FROM
systables
WHERE
tabid = 1;
And it returns:
today_zerohour yesterday_dawn today_dawn
2016-10-21 00:00:00 2016-10-20 04:00:00 2016-10-21 04:00:00
So, what is happening here:
The operator TODAY returns the system date as a DATE type. The DATE type does not have the precision I want (it only has year, month and day), so I cast the value (cast operator is ::) to a DATETIME with precision from year to second (the hour, minutes and seconds are set to zero):
TODAY::DATETIME YEAR TO SECOND
In Informix, for an addition or subtraction with a DATETIME value to return another DATETIME value, I need to add or subtract an INTERVAL value. So I created 2 INTERVAL values.
One INTERVAL of 20 hours to subtract from the today value (again the cast operator :: is used, this time to cast from a string to an INTERVAL):
'20:00:00'::INTERVAL HOUR TO SECOND
One INTERVAL of 4 hours to add to the today value:
'04:00:00'::INTERVAL HOUR TO SECOND

How to get date of particular week in Rails

I am working on Rails application where I am trying to fetch date of particular week but week start from not 1 January of year but some fixed date.
Like, my week start from 8 july 2016 (08-07-2016) so now i want to fetch start date and end date of any week.
Means week 1 start date -> 08-07-2016 and end date -> 14-07-2016.
Now i want to fetch any week start date and end date but how? I already tried but got solution of year start date not like this.
Any one have idea?
Thanks in advance.
Ok friends,
I found my answer below
week = 3
number = (week-1) * 7
start_date = Date.new(2016, 7, 8) +number.to_i
d = start_date.to_s.split('-')
end_date = Date.new(d[0].to_i, d[1].to_i, d[2].to_i) + 6
end_date = end_date.strftime("%Y-%m-%d")
Let me know if any one have confusion in this answer.
There is no need to split the start date and create a new date object.
start_date_of_first_week = Date.new(2016, 7, 8)
week_number = 3
start_date = start_date_of_first_week + ( (week_number - 1) * 7 )
end_date = start_date + 6

Stored Procedure to check leap year when entering the date

I want to write a procedure which will check whether entered date is a leap year or not.Kindly help me with this.
The following scalar function takes in a year and returns a bit flag indicating whether the passed in year is a leap year or not.
create function dbo.fn_IsLeapYear (#year int)
returns bit
as
begin
return(select case datepart(mm, dateadd(dd, 1, cast((cast(#year as varchar(4)) + '0228') as datetime)))
when 2 then 1
else 0
end)
end
go
Here are a few examples:
select dbo.fn_IsLeapYear(1900) as 'IsLeapYear?'
select dbo.fn_IsLeapYear(2000) as 'IsLeapYear?'
select dbo.fn_IsLeapYear(2007) as 'IsLeapYear?'
select dbo.fn_IsLeapYear(2008) as 'IsLeapYear?'
Explanation:
Giving the date as input to your custom sql function
The function takes in the year, appends '0228' to it (for February 28th) and adds a day.
If the month of the next day is a 2 (as extracted by the DATEPART function), then we're still in February so it must be a leap year! If not, it is not a leap year.

Resources