I want to add word Week to show as Week then number from function MOD i tried the following
select EVENTTIMESTAMP, Year (EVENTTIMESTAMP) as Year, QUARTER (EVENTTIMESTAMP) as Quarter, 'Week' MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as WeekNoQuarter
but i get error :
An unexpected token "MOD" was found following ") as Quarter, 'Week'". Expected tokens may include: ",".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.16.53
any suggestion on way to put word Week.
Thanks!
In your code you are trying to name a string 'Week' as MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as WeekNoQuarter. Try concatenating ( || ) the String 'Week ' with the calculated value. Since the calculated value is of type int you will have to cast it:
'Week ' || cast(MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as char(2)) as WeekNoQua...
Related
I'm having an issue using the ACCESS DateDiff function. I'm attempting to obtain the FULL months between two dates however this function ignores dates in the middle of the month. It apparently performs a simple calculation on the month number itself and does not take into account the day of the month.
For example: If I want to know the full month between October 16th and November 3rd. This should return zero however the DateDiff function is returning 1. What code do I need to implement in order to obtain the full months between and have it work for ALL months. I say ALL months as months can have 28 days (29 in a leap year), 30 days and 31 days which is why I wouldn't simply calculate the days between two dates.
Access Function: DateDiff("m",PAID_DATE,STATIC_DATE) AS Months_Between
The screenshot below shows the data I want as well as the incorrect month between results I am obtaining.
Lastly a screenshot of a simple representation of my output in the ACCESS Design View.
Oracle Example of what I'm trying to accomplish in ACCESS
I can accomplish this in Oracle very easy as oracle's Months_Between function takes into account the months and leap years and the days within each month and outputs the actual months between value. If I knew how to get this in ACCESS I wouldn't have posted this question.
You'll need a custom function like this:
Public Function Months( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booLinear As Boolean) _
As Integer
' Returns the difference in full months between datDate1 and datDate2.
'
' Calculates correctly for:
' negative differences
' leap years
' dates of 29. February
' date/time values with embedded time values
' negative date/time values (prior to 1899-12-29)
'
' Optionally returns negative counts rounded down to provide a
' linear sequence of month counts.
' For a given datDate1, if datDate2 is decreased stepwise one month from
' returning a positive count to returning a negative count, one or two
' occurrences of count zero will be returned.
' If booLinear is False, the sequence will be:
' 3, 2, 1, 0, 0, -1, -2
' If booLinear is True, the sequence will be:
' 3, 2, 1, 0, -1, -2, -3
'
' If booLinear is False, reversing datDate1 and datDate2 will return
' results of same absolute Value, only the sign will change.
' This behaviour mimics that of Fix().
' If booLinear is True, reversing datDate1 and datDate2 will return
' results where the negative count is offset by -1.
' This behaviour mimics that of Int().
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of months to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2010-03-30. Cactus Data ApS, CPH.
Dim intDiff As Integer
Dim intSign As Integer
Dim intMonths As Integer
' Find difference in calendar months.
intMonths = DateDiff("m", datDate1, datDate2)
' For positive resp. negative intervals, check if the second date
' falls before, on, or after the crossing date for a 1 month period
' while at the same time correcting for February 29. of leap years.
If DateDiff("d", datDate1, datDate2) > 0 Then
intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
intDiff = Abs(intSign < 0)
Else
intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
If intSign <> 0 Then
' Offset negative count of months to continuous sequence if requested.
intDiff = Abs(booLinear)
End If
intDiff = intDiff - Abs(intSign < 0)
End If
' Return count of months as count of full 1 month periods.
Months = intMonths - intDiff
End Function
Okay the following where clause works, except in January when the calculation is turning up an invalid date:
WHERE
(DATE((DIGITS(LNYYP2) || '-' || DIGITS(LNMMP2) || '-' || DIGITS(LNDDP2))) >
((CURRENT DATE - DAY (CURRENT TIMESTAMP) DAYS)) - 13 MONTH)
AND DIGITS(SHFP02.LNYYP2) || '-' || DIGITS(LNMMP2) <>
YEAR (CURRENT TIMESTAMP) || '-' || MONTH (CURRENT TIMESTAMP)
Potential issue:
For the second condition,
(DATE((DIGITS(LNYYP2) || '-' || DIGITS(LNMMP2) || '-' || DIGITS(LNDDP2))) >
((CURRENT DATE - DAY (CURRENT TIMESTAMP) DAYS)) - 13 MONTH)
You're comparing two strings and not two dates or two sets of numbers. I'm not sure that's your issue because you don't give any sample data that gives right or wrong answers, but maybe it could be better written as:
and (lnyyp2, lnmmp2) not in (values(int(year(current date)), int(month(current date)))
Sorry, it looks like you are using a different DB than what I thought. This is an answer for how to solve this issue in SQL Server.
Original Anser:
I am not sure exactly what is going on in that where clause, but you can get 12 months ago with DATEADD. If you need to get the first of the month, you can build it with DATEFROMPARTS.
declare #today date;
declare #past date;
set #today = SYSDATETIME();
set #past = dateadd(month, -12, #today)
select #today, #past, DATEFROMPARTS(YEAR(#past), MONTH(#PAST), 1)
I have searched, but all results didn't help me to understand.
I need to select names of people who are 18-23 years old.
So my try was:
WHERE ((People.Birth) Between (Now()-Year(18)) And (Now()-Year(23)))
What I'm doing wrong? Solution as #some_date# is a bad idea!
For a true solution, you need to use DateAdd and a function like this:
Public Function AgeSimple( _
ByVal datDateOfBirth As Date) _
As Integer
' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.
Dim datToday As Date
Dim intAge As Integer
Dim intYears As Integer
datToday = Date
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDateOfBirth, datToday)
If intYears > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of datDateOfBirth.
intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
End If
AgeSimple = intAge
End Function
Then your query will have this where clause:
WHERE AgeSimple(People.Birth) Between 18 And 23
Finally solved it by simple
WHERE ((People.Birth) Between (Now()- 365*18) And (Now()-365*23))
If you have better solution- welcome
Hi I am creating a table in MS Access to store the details of children in a school.
I have a field called YearGroup which needs to calculate the school year they are in based on their date of birth and whether they have been moved up or down a year.
I.e. if the expression deems they are six years old they should be placed in year 2. If they were moved down or up a year they should be in year 1 or 3 (this is based on another field in the table called YearModifier).
The code I have at the moment is this:
Year(Now()) - IIf(Month([DOB]) > 8, Year([DOB]) + 6 + [YearModifier], Year([DOB]) + 5 + [YearModifier])
My problem is that Year(Now()) is returning as invalid expression. Lots of websites have recognised using the Now() function and also I've tried Date() but nothing seems to be accepted by Access (The version is 2010).
What is going on? How can I get today's date in a calculated field expression?
Thanks
Try creating a query with all of the fields from your table, and then add an extra field YearGroup: Year(Now()) - IIf(Month([DOB]) > 8, Year([DOB]) + 6 + [YearModifier], Year([DOB]) + 5 + [YearModifier])
It appears that Date functions can't be used in calculated columns in tables.
You could use this to get the year, which might work in field expressions:
format(date(),"yyyy")
About your function (which I have re-written very slightly)
Year( Now() )
- Year([DOB])
- IIf( Month([DOB]) > 8
, 6
, 5 )
+ [YearModifier]
however!
I don't think you want to use now(). Which year they are in depend on their age at the 1st Sept at the start of the current academic year not now! Ok now will work until 31/dec/2015, so I must assume you will not be using the function after this date!
If you are you must use 2015 not Now().
Ok?
You can calculate the age of the children with a simple function:
Public Function AgeSimple( _
ByVal datDateOfBirth As Date) _
As Integer
' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.
Dim datToday As Date
Dim intAge As Integer
Dim intYears As Integer
datToday = Date
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDateOfBirth, datToday)
If intYears > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of datDateOfBirth.
intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
End If
AgeSimple = intAge
End Function
Then your expression would be something like this (ignoring the modifier):
ClassYear: IIf(AgeSimple([DOB]) > 6, 2, 1)
I am trying to fix a function which returns the number of weeks in a given year.
Here's how it looks:
Function GetWeekNo(date)
weekOfYear = DatePart("ww", DateValue(date), vbMonday, vbFirstFourDays)
If weekOfYear > 52 Then
If DatePart("ww", DateValue(date) + 7, vbMonday, vbFirstFourDays) = 2 Then
weekOfYear = 1
End If
End If
GetWeekNo = weekOfYear
End Function
When this function is given the date 12-31-2010 it returns 52. There are 53 weeks in 2010.
Note: I have no experience with classic ASP, what-so-ever.
Seems like it depends on which week is considered as the "first week of the year".
DatePart( "ww", "12/31/2010", vbMonday )
' returns 53
' FirstWeekOfYear parameter defaults to vbFirstJan1
' the week that contains January/01/2010
' here, its the week starting on December/28/2009
DatePart( "ww", "12/31/2010", vbMonday, vbFirstFourDays )
' returns 52
' FirstWeekOfYear parameter set to vbFirstFourDays
' the first week that has at least four days of the new year
' here, its the week starting on January/04/2010
DatePart( "ww", "12/31/2010", vbMonday, vbFirstFullWeek )
' returns 52
' FirstWeekOfYear parameter set to vbFirstFullWeek
' the first week that has full seven days of the new year
' here, again, its the week starting on January/04/2010
Decide what is your definition of the first week of the year, then use the DatePart function accordingly.