I am trying to write an IF statement but am getting 1 error - any help would be greatly appreciated.
I need the statement to say: If Prepared Date is after Prepared Deadline, then return the number of days between the 2 dates. If Prepared Date is before or equal to Prepared Deadline, then return "0", Also, if Prepared Date is null, return return the number of days between Preparer Deadline and the current date.
if [Prepared Date] > [Prep Deadline] then datediff('day',[Prep Deadline],[Prepared Date])
elseif [Prepared Date] <= [Prep Deadline] then "0"
elseif ISNULL([Prepared Date]) then datediff('day',[Prep Deadline],TODAY())
end
enter image description here
Two errors are clear. The first is described in the error message. Don’t mix strings and ints, so lose the quotes around zero to turn it into an int. The second is you should test for null values in a field first, before using it in an expression. Lastly, since if/else if tests are evaluated in sequence, you don’t need to repeat the comparison on the third branch - you can just use an else.
So I’d try
if isnull([Prepared Date]) then
datediff('day',[Prep Deadline],today())
elseif [Prepared Date] > [Prep Deadline] then
datediff('day',[Prep Deadline],[Prepared Date])
else
0
end
You can simplify further by using the IFNULL() function to supply a default value in situations where a field is null, and the MAX() function to make sure you never return negative numbers.
max(datediff('day',[Prep Deadline], ifnull([Prepared Date], today())), 0)
Its a matter of taste which you prefer -- logically both expressions are equivalent.
Related
I'm calculating the diff between two Dates:
let diff = Calendar.current.dateComponents([.day], from: oldDate, to newDate)
and I got a DateComponents as a result, in which the day property is an optional, the doc doesn't say it's guaranteed to have a non-nil day in this calculation, so I wonder if I can make the assumption that day will never be nil in this case, even if the two Date are the same, day should be 0 instead of nil; Or, is it possible to have a day which equals to nil?
Thanks!
I'd say the contract for the function call is ambiguous. A DateComponent's properties are all optional. It seems like it should return a value in the days property when that's what you ask for, but I'd still code defensively.
I call the force-cast operator the "crash if nil" operator, because that's what it does.
I have two JQL queries:
due >= "0"
due >= now()
Is there a difference? I cannot find a reference description for what "0" means.
Thank you.
No, there is no difference. The second query
2. due >= now()
compares due to the current date. The first query
1. due >= "0"
compares due to the current date MINUS "0" offset days or weeks, which is again equal to the current date.
Compare the Atlassian Advanced searching - fields reference page:
Or use "w" (weeks) or "d" (days) to specify a date relative to the current date. Be sure to use quote-marks (").
I need to know how would I check if my given value is between two closest array's members. For example I have an array of dates with the date of week start in given period of time. And I need to check if my given date is in one of its week. For example:
2015-11-02
2015-11-09
2015-11-16
2015-11-23
And my given value is 2015-11-11 for example. How should I check if it is one of these weeks date? Thanks for help.
%w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).any? do |date|
date.to_date.cweek == Date.today.cweek
end
And here is what this does:
First, you have an array of strings, you use any? to loop through it and check if any fulfils a requirement, then you cast you date strings into actual dates, and cweek gives you the number of a week in the year. Date.today gives you today's date.
Instead of Date.today.cweek you can use '2015-11-11'.to_date.cweek.
The loop above returns boolean; you could also get an array of values that fulfil a condition like this:
new_array = %w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).map do |date|
date.to_date.cweek == '2015-11-11'.to_date.cweek
end.compact
Resources:
Date class on ruby-doc.org
Date & Time in Ruby on tutorialspoint.com
UPDATE
If you want to get from the database only records with a date from particular week, this is how you could do it:
my_date = '2015-11-11'.to_date
matching_records = MyResource.where( date: my_date.beginning_of_week..my_date.end_of_week )
The assumptions are that you have a model MyResource, and that it has a column date. What this does is returns a relation with all the records that have dates from the same week as my_date.
Assuming your dates array is sorted:
date >= dates.first && date <= dates.last
If you're dealing with strings, you can "require 'date'" and transform your strings to dates ("Date.parse('2001-02-03')").
As others have suggested, you can then see if your date is between the first and last entry of your list.
If the real list is sorted and each entry is one week apart, then you can easily find where in the list your guy is.
E.g., say the list is [date_0, date_1, date_2, ..., date_k] (all 1 week apart), and you're given a test_date between date_0 and date_k. Then (test_date.jd - date_0.jd)/7 gives you the index of the date in your list that is <= test_date.
I am working on some SQL statements for an ASP.NET application. One of the things that is required is to only display information in an open period. The period is updated automatically by the vendor software as the previous period is closed. So I finding myself doing a bunch of sub selects like:
where date >= (SELECT DATE(CONCAT('20', CONCAT(YY, CONCAT('-', CONCAT( MM, (CONCAT('-', DD))))))) FROM LIB/FILE')
Yes, each portion of the date is in separate fields.
Would making this query a function make the query more efficient? I have never created a function before how would I do that? My thought is having something like:
IsInRange(date)
So I can do WHERE IsInRange(date).
Or is there a better way?
CREATE FUNCTION toDate(yy CHAR(2), mm CHAR(2), dd CHAR(2))
RETURNS DATE
RETURN DATE('20' || '-' || yy || '-' || mm || '-' || dd)
SELECT *
FROM table
WHERE date <= toDate(yy, mm, dd)
Yes, but the point of the function would be for code reuse. If you are doing this in other places, or anticipate that then go for it.
For now I created a view to join the fields into a single date, this at least cleaned up the query a bunch.
For some reason:
Analytic.where({:ga_date.gte => '2010-09-01'}).count() # greater than or equal to
gives back 0, but
Analytic.where({:ga_date.gte => Time.parse('2010-09-01')}).count()
gives back 230, which is the number of records (documents).
Actually, the first line on the top works in another case, so it is quite strange.
Can only the date be compared, because if it is
Analytic.where({:ga_date.lte => Time.parse('2010-09-10')}).count() # less than or equal to
then all the records with date 2010-09-10 will not be counted because Time.parse('2010-09-10') will give 2010-09-10 00:00:00, so the records will all have to be 2010-09-09 before the midnight. In other words, 2010-09-10 2am won't be included because 2am is not "less than or equal to" 00:00:00. It can be hacked by using
Analytic.where({:ga_date.lte => Time.parse('2010-09-10 23:59:59')}).count()
but it is kind of ugly. If there is a way to compare by date only like the first line of code in this post?
I think that you have two separate issues here.
Different data types
The following two lines are not equivalent. The first is a string comparison. The second is a comparison with a date object.
Analytic.where({:ga_date.gte => '2010-09-01'}).count()
Analytic.where({:ga_date.gte => Time.parse('2010-09-01')}).count()
I think you have figured this out, but it's important to be clear here. If you are storing date objects in the DB, you need to perform comparisons with date objects.
MongoDB will compare types and data.
Mismatch date storage
You are storing dates that have information for hours, minutes and seconds. However, you don't like the following notation:
:ga_date.lte => Time.parse('2010-09-10 23:59:59')
The workaround here is to use $lt and the day after.
:ga_date.lt => (Time.parse('2010-09-10') + 1.day) # or (60 * 60 * 24)
to add,
it is not strangely works, its coincidentally works when it just happens the string representation of the date happens to also lexicographically be 'greater than' the other date
other issue,
try to use only as much data fields as needed
if you meant it to be "within the calendar day",
what I usually like is to call beginning_of_day in both cases to equalize
this has the effect of neutralizing the minutes
else if you really meant within a 24h strike zone,
use ActiveSupport's '+ 1.day'