I'm working with DB2 database in which the time field is the following string:
12104102000000
where it means, from the left:
> 1 --> is the century 21 --> is the year 04 --> is the month 10 --> are
> the hours 20 --> are the minutes 00 --> are the seconds 000 --> are
> the milliseconds
Well, the problem is that this field is synchronized with the reference time, so respect to Italy (where I'm working) is two hours back.
I have a query where usually I extract the date field with substr function and the starting part is:
select substr("Message_Time",4,2) || '/' || substr("Message_Time",6,2) || '/' || '20' || substr("Message_Time",2,2) as "Date",
substr("Message_Time",8,2) || ':' || substr("Message_Time",10,2) as "Hour"
I need to add to hour substr only, the 2 hour to bring the date to Italy.
But I think the issue is at the change of the day, because if I have e.g. 20th of April, 22:40, it corrisponds to 21st of April, 00:40 in Italy.
How should I do in order to make it working?
Please tag which DB2 you use.
If DB2LUW or DB2 for I, you can use
to_date('12104102000000', 'YYYMMDDHH24MISSFF1') - 100 YEARS + CURRENT TIMEZONE
I guess it works with DB2 for Z but I'm not sure.
Try this, if you want your result to be in the same string form:
SELECT
SUBSTR (D, 1, 1)
|| SUBSTR
(
TO_CHAR
(
TO_DATE(CAST(19 + SUBSTR(D, 1, 1) AS CHAR(2)) || SUBSTR(D, 2), 'YYYYMMDDHH24MISSFF3') + 2 HOURS
, 'YYYYMMDDHH24MISSFF3'
)
, 3
)
FROM (VALUES '12104102240000') T (D)
Related
I'm quite a beginner on VB/SQL, I just began my learning few months ago, but I can understand the logic of algorithms as I used to do some Excel VBA .
I'm actually designing a database where I can (wish to) follow up every colleague's activity during the year.
The objective is to have a (Monthly) ratio of =>
Billable days / (Billable + Non Billable - Absent)
The context :
A single person can be : Working internally (Non billable), OR Working Externally (Billable) , OR on Holidays (Absent).
I have a [Planning] Table where it stores the following data : [Consultant_ID] (linked to another table [Consultant], [Activity] (A list with the three choices described above), [Beginning_Date], [End_Date].
Example :
Consultant 1 : Working externally from 01/01/2019 to 01/06/2019,
Working internally from 02/06/2019 to 31/12/2019,
Holidays from 02/03/2019 to 15/03/2019
Is there a way to have the Billable ratio of March for example ?
I created 4 queries (Maybe too much ?)
3 queries : [Consultant_ID] [Activity] [Beginning_Date] [End_Date] [Ratio : Datediff("d";[Beginning_Date];[End_Date]).
For each query : The [Activity criteria] : one Working Internally, one Working Externally, one Absent.
And for the [Beginning_Date] and [End_Date] criterias : <=[Enter beginning date], >=[Enter End date]
And the 4th query [Consultant ID] [Billable] [Non billable] [Absent] (and planning to add the [RATIO]).
Problem is : the Datediff counts the dates of the whole activity of what it finds, and not only the dates between 01/03/2019 and 31/03/2019 as I wish to.
I Expect the output of the ratio to be : Billable days / (Billable + Non Billable - Absent) of the desired period.
The actual output shows the billable, non billable, and absent days of the whole period between the dates which are inputted
So instead of 31 Billable, 0 Non billable, 15 Absent
It shows 180 Billable, 0 Non Billable, 32 Absent
Sorry for the long post, it is actually my first, and thank you very much !
I've been struggling with this for a whole week
We first need to figure out the maxBegin and the minEnd dates for each row
SELECT
*,
(IIF (Beginning_Date > #3/1/2019#, Beginning_Date, #3/1/2019#) ) as maxBegin,
(IIF (End_Date < #3/31/2019#, End_Date, #3/31/2019#) ) as minEnd,
Datediff("d", maxBegin, minEnd) + 1 as theDiff
FROM Planning
Where Beginning_Date <= #3/31/2019# AND End_Date >= #3/1/2019#
Then use that to compute the durations. Note: DateDiff does not count both ends, so we need to add +1.
SELECT
Consultant_ID,
SUM(IIF (Activity = "Working Internally", Datediff("d", maxBegin, minEnd) +1, 0) ) as NonBillable,
SUM(IIF (Activity = "Working Externally", Datediff("d", maxBegin, minEnd) +1, 0) ) as Billable,
SUM(IIF (Activity = "Holidays", Datediff("d", maxBegin, minEnd) +1, 0) ) as Absent
FROM
(
SELECT
*,
(IIF (Beginning_Date > #3/1/2019#, Beginning_Date, #3/1/2019#) ) as maxBegin,
(IIF (End_Date < #3/31/2019#, End_Date, #3/31/2019#) ) as minEnd
FROM Planning
Where Beginning_Date <= #3/31/2019# AND End_Date >= #3/1/2019#
) as z
GROUP BY Planning.Consultant_ID;
Finally, you need to substitute the actual Begin/End dates via params into the sql to run your query. Also note that the Holidays are only 14, not 15.
Also, you can add the Ratio calculation right into this sql, and have only one query.
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)
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 create a custom function of given a date in Filemaker, I would like it to determine if this week of fiscal year number is within the first week of quarter else it will calculate the first date next quarter. Our Fiscal year starts on July 1
So for defined requirements our FY starts on July 1 and Qtrs are on week # 1, 14, 27, 40 our weeks go from 1-52 and the week starts on Tuesday (Defined as day 3). If FY starts on Monday than the first week will be from Mon-Tues (Therefore a shortweek) then week 2 will be a full 7 day week.
Example---> If I have a date 09/09/2011 that would be week 11 in Q1, therefore since it is not the first week of the quarter I would like the following date for the next Qtr which would be Wk 14 first date of 9/27/2011. So my evaluation needs to determine whether the given date is within the first week of a qtr (weeks 1, 14, 27, 40) or provide the first week of the next qtr.
Also here is the initial CF I was working with from Brian Dunnings site.
https://www.briandunning.com/cf/147
I know this would be developed in filemaker but there maybe something developed in another language which may apply...
Thanks in advance
Try this as your starting point:
Let ( [
startFY = Date ( 7 ; 1 ; Year ( Datefield ) - ( Month ( Datefield ) < 7 ) ) ;
firstTuesday = startFY - Mod ( startFY - 2 ; 7 ) ;
fiscalWeek = Div ( Datefield - firstTuesday ; 7 ) ; //numbering starts at 0
targetWeek = 13 * Ceiling ( fiscalWeek / 13 )
] ;
firstTuesday + 7 * targetWeek
)
Note that the result is always a Tuesday; you may want to adjust this for the boundary cases of fiscal year start and end. The way it works now, you'll get a result of July 30, 2015 for both June 15, 2015 and July 6, 2015.
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.