I have a date column called "Expiry_Date". What I've been asked to do is retrieve an integer based on the month relative to the current, for example:
Current Date: 7th May 2016
MyTable
----------
1) 12th May
2) 31st May
3) 2nd June
4) 11 June
5) 19 June
6) 28th July
7) 1st August
The result would be:
1) 1
2) 1
3) 2
4) 2
5) 2
6) 3
7) 4
So, it's getting the month number relative to the current for the given date (or periods). This also has to work across years so "1 January 2017" would be "9" etc.
Many thanks
Perhaps the function MONTHS_BETWEEN solves.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions089.htm
Something like:
round(months_between(expiry_date, date '2016-05-07') + 1)
Related
Suppose the flow of time is subdivided into periods:
Period 1 beginning August and ending January
Period 2 beginning February and ending July
Note, that each period is 6 month.
Question
Now suppose two dates A and B are given.
Could you provide an expression (closed formula), which computes the
number of periods where A is within the first and B is within the last
period?
'Closed formula' :: The creation of a procedure isn't an option her.
Let's have a detailed look
Let...
A be the 2nd of March 2015
B be the 15th of January 2018
Thus...
A is part of period 2 beginning February 2015.
B is part of period 1 beginning August 2017.
Therefore...
the difference in periods between A and B is 6.
Since
Period 2 beginning February 2015
Period 1 beginning August 2015
Period 2 beginning February 2016
Period 1 beginning August 2016
Period 2 beginning February 2017
Period 1 beginning August 2017
Try looking into the VBA function DateDiff https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/datediff-function
As formula (assuming your date in A2 and B2):
=(YEAR(EDATE(A2;-1))*2+INT((MONTH(EDATE(A2;-1))-1)/6)) - (YEAR(EDATE(B2;-1))*2+INT((MONTH(EDATE(B2;-1))-1)/6))
As VBA function:
Function datePeriods(ByVal d1 As Date, ByVal d2 As Date) As Long
d1 = DateAdd("m", -1, d1)
d2 = DateAdd("m", -1, d2)
Dim p1 As Long, p2 As Long
p1 = Year(d1) * 2 + ((Month(d1) - 1) \ 6)
p2 = Year(d2) * 2 + ((Month(d2) - 1) \ 6)
datePeriods = p2 - p1
End Function
The basic idea is to "move" the periods to the first and second half of a year, then calculate a numeric value for the year (year * 2) and the "half year" (integer division by 6) and subtract these two values.
Code to test your function. Insert a module in the VBA editor and enter the following code in it:
Option Explicit
Public Function CountPeriods(startDate As Date, endDate As Date) As Long
Dim diffMonths As Long
Dim diffPeriods As Long
diffMonths = DateDiff("m", startDate, endDate)
diffPeriods = diffMonths \ 6
CountPeriods = diffPeriods
End Function
Now enter your start and end dates and refer to the code above like this in cell B1 (or any cell):
I have some 5 digits dates in a format that the food industry generally calls "Julian" dates (Warning: these are not the "Julian" dates you are familiar with. It's misused terminology that "stuck" and became the standard in the industry. Don't comment on that, it's just how it is.)
The first 3 digits of these "Julian" dates are a number representing the day of a year.
Example:
January 1 = 001
January 2 = 002
December 31 = 365 or 366
The next two digits are the last two digits of the year. 2015 = 15.
For example 22215 = August 10, 2015 (I believe).
I need an Oracle SQL statement that convers these "dates" into standard dates to join to other date data.
I'd add (count of days) -1 to 1st Jan of year
select to_date('20'||15||'01.01','yyyy.mm.dd') + 222-1 from dual
10/08/2015
A co-worker found the answer. The "DDD" format:
SELECT to_date('36515', 'DDDYY') FROM DUAL
I am currently making an excel model where the date is an important aspect of it. However as the information comes from a different source it is sometimes structured in such a way that it is not realistic.
I have a problem with the calculation of the year and week combination when adding weeks or subtracting them.
The start data looks like this:
yyyyww (example: 201525, year 2015 week 25)
Now if I want to add for example 3 weeks, I can just do that by adding 3, results is 201528. However when it comes to 201552 (and sometimes yyyy53) it is more difficult as I need to calculate to 2016.
The same goes for when calculating back in the time, towards 2014 or lower. Does anyone know or have a solution for this? Maybe in VBA? or a formula trick?
a formula to add 1 to your week number format looks like this:
=YEAR(DATE(LEFT(A2, 4), 1, 1) + MID(A2, 5, 2) * 7) * 100 +
WEEKNUM(DATE(LEFT(A2, 4), 1, 1) + MID(A2, 5, 2) * 7)
it will create a sequence of week numbers that includes: 201452 => 201453 => 201502
LEFT(A2, 4) and MID(A2, 5, 2) extract the year and week components
date(year, 1, 1) + (week - 1) * 7 converts that to an actual Excel Date value, e.g. 201502 => 2015-01-08
date(year, 1, 1) + week * 7 will create a date that is 1 week after the "current" date
year(date) * 100 + weeknum(date) converts that date back to yyyyww format
I have a query with an IIF() expression in one column that I am using to identify if a date is previous to the current month to then amend it if so. So if I run the query on 19th March 2014 and the EffFrom date is before 1st March 2014, I would want that column entry to now appear as 1st March 2014.
I am using the below expression which is pretty much doing what I want, however I know it is not considering the year -- i.e. it is changing an entry of 1st Jan 2015 to be 1st March 2014.
EffFrom:
IIf(Month([Table.Efffrom])"Less than symbol"Month(Date()),Date()-Day(Date())+1,[Table.Efffrom])
Can someone correct the expression for me?
I interpreted "if I run the query on 19th March 2014 and the efffrom date is before 1st March 2014 I would want that column entry to now appear as 1st March 2014" to mean you want something like this from a query run today (Mar 19th 2014):
id Efffrom adjusted_date
1 1/1/2014 3/1/2014
2 3/1/2014 3/1/2014
3 3/31/2014 3/31/2014
4 1/1/2015 1/1/2015
If that is correct, your IIf expression can use DateSerial to check whether Efffrom is before the first of this month, and transform the older dates.
SELECT
y.id,
y.Efffrom,
IIf
(
y.Efffrom < DateSerial(Year(Date()), Month(Date()), 1),
DateSerial(Year(Date()), Month(Date()), 1),
y.Efffrom
) AS adjusted_date
FROM YourTable AS y;
Here's what i do to get the first day of the next month
EffFrom: DateAdd("m",1,CDate(Format([Table.Efffrom],"m/\1/yy")))
I have a table with 3 columns: user, value, and date. The main query returns the values for a specific user based on a date range:
SELECT date, value FROM values
WHERE user = '$user' AND
date BETWEEN $start AND $end
What I would like is for the results to also have a column indicating the week number relative to the date range. So if the date range is 1/1/2010 - 1/20/2010, then any results from the first Sun - Sat of that range are week 1, the next Sun - Sat are week 2, etc. If the date range starts on a Saturday, then only results from that one day would be week 1. If the date range starts on Thursday but the first result is on the following Monday, it would be week 2, and there are no week 1 results.
Is this something fairly simple to add to the query? The only ideas I can come up with would be based on the week number for the year or the week number based on the results themselves (where in that second example above, the first result always gets week 1).
Example:
$start = "05/27/2010";
$end = "06/13/2010";
//Query
Result:
week date user value
1 05/28/2010 joe 123
3 06/07/2010 joe 123
3 06/08/2010 joe 123
4 06/13/2010 joe 123
This can easily be done with this simple and obvious expression :)
SELECT ...,
FLOOR(
(
DATEDIFF(date, $start) +
WEEKDAY($start + INTERVAL 1 DAY)
) / 7
) + 1 AS week_number;
Some explanation: this expression just calculates difference between the given date and the start date in days, then converts this number to weeks via FLOOR("difference in days" / 7) + 1. That's simple, but since this works only when $start is Sunday, we should add an offset for other week days: WEEKDAY($start + INTERVAL 1 DAY) which equals to 0 for Sun, 1 for Mon, ..., 6 for Sat.