Comparing dates [duplicate] - objective-c

This question already has an answer here:
Comparing dates [duplicate]
Know if a date(weekday) is bettween two other dates(weekedaysÂș)
(1 answer)
Closed 9 years ago.
In my application I need to compare three dates in one if-else cycle. I have a start date, end date and the date of testing. What I want to check is whether the test date is between the start date and end date.
if (testDate >= startDate && testDate <= endDate) {
If I set the startDate to 25/04/2012 and the endDate to 24/04/2019 and the testDate to 25/12/2013 the code works. But if i set the startDate to 26/04/2000 and the endDate to 24/04/2019 and the testDate to 25/12/2013 the code does not works. What's wrong?

You cannot sanely compare Objective-C objects with any boolean operators in C. You'd be comparing their addresses, not their values.
Look at the documentation for the NSDate class, specifically the -compare: method.

Dates can be compared like that. However, you must first call timeIntervalSince1970 to convert the date to an integer type value. In this case, it gives you a numeric value of type NSTimeInterval.
The other option is to use the compare: method invoked on the NSDate object.
In that case, comparing one date to another will give a result detailed in the class reference as follows:
The receiver and anotherDate are exactly equal to each other,
NSOrderedSame
The receiver is later in time than anotherDate, NSOrderedDescending
The receiver is earlier in time than anotherDate, NSOrderedAscending.

Related

Unable to get data between two years

I am not getting data between two years, below is between condition
to_char(Wfc.APPLYDTM,'MM/DD/YYYY') between '12/11/2019' and '01/10/2020'
but I am getting data between '12/11/2019' and '12/31/2019' & '01/11/2020' and '01/01/2020' for these dates but not between two different years.
Please help
Try using TO_DATE instead of TO_CHAR, and then compare against valid Oracle date literals:
SELECT *
FROM Wfc
WHERE TO_DATE(APPLYDTM, 'MM/DD/YYYY') BETWEEN date '2019-12-11' AND date '2019-01-10';
Note that if APPLYDTM already be a date, then you don't need to call TO_DATE on it. It doesn't make sense to convert your data to character, if you intend to work with it as a date.
You should convert your data to Date to be able to compare correctly.
The main idea is you should compare date value instead of string value.
to_date(Wfc.APPLYDTM,'MM/dd/yyyy') between to_date('12/11/2019','MM/dd/yyyy') and to_date('01/10/2020','MM/dd/yyyy')
Read here to more details.
Do not convert date/time values to strings! Use the built in functionality.
Your logic is most simply expressed as:
Wfc.APPLYDTMbetween >= DATE '2019-12-11' AND
Wfc.APPLYDTMbetween < DATE '2020-01-11'
Note that the date constants are provided using the DATE keyword. This supposed ISO 8601 standard date formats (happily!).
Also note the use of >= and < rather than BETWEEN. The date data type in Oracle can include a time component -- even if you don't see it when you query the table. This ensures that all date/times are included in the range.
As an added benefit, this can use an index on (APPLYDTMbetween). Using a function usually precludes using an index, unless you have defined a function-based index.

How to select records between two dates in Access? [duplicate]

This question already has an answer here:
Query to compare between date with time and date without time - python using access db
(1 answer)
Closed 4 years ago.
I need to select records with DateTime between two dates in an Access query. The problem is that when I'm execute this query:
select * from logs
where date_added >= CDate("01/10/2018")
AND date_added <= CDate("04/10/2018")
I need both border values but the result does not include the last day. Maybe because "04/10/2018" is converted to "04/10/2018 00:00:00" and this value is less than all date_added values of that day.
Can I convert date_added to date only?
Do you can add a day to your date?
AND date_added < DateAdd('d',1,CDate("04/10/2018"))
An alternative expression:
SELECT * FROM logs
WHERE DateValue(date_added) BETWEEN #01/10/2018# AND #4/10/2018#
Useful date functions and syntax:
Date literals can be delimited with # in both VBA code and SQL statements, so you don't have to call CDate() on string values. Examples: #10/6/2018 4:16 PM#, #1/1/2018#
Simple mathematical notation can be used to add and subtract whole days from a date value. Example: #10/6/2018# + 1 == #10/7/2018#
DateValue( val ) takes arguments of various formats and returns a date/time value with only the date portion. This answers your question Can I convert date_added to date only? It essentially returns the same date value with the time portion as 00:00:00.
Example: DateValue(#10/6/2018 4:16 PM#) == #10/6/2018#
DateAdd ( interval, number, date ) as already noted by Milad Aghamohammadi.
Within SQL only (not VBA), one can use the BETWEEN operator. It works with various data types that have a natural sort order, which includes date values.
Example ... WHERE [DateField] BETWEEN #1/1/2018# AND #4/1/2018#

Passing a user prompt as a date (or even a string) in Oracle SQL

I am using Business Objects, which runs on top of an Oracle SQL database. I do not have access to PL or any kind of SQL command line, and I do not have write access to the database. I can only run queries as single commands, requiring a defined set of columns to be output.
I am able to take data from user prompts, which appear in the SQL as:
#variable('Prompt1')
For example I can say:
SELECT
A.SomeDate
FROM
A
WHERE
A.SomeDate BETWEEN #variable('Start') AND #variable('End Date')
This is easy enough. It runs; requests the user to input some dates; and then returns all matches which are between them (inclusive).
The problem is, however, the users will be using Business Objects' "Infoview" system to run the queries, and the prompt system presents a date picker - which by default includes the time portion of the date ("01/01/2016 12:00:00 AM").
If the user does not delete the time portion, it can cause records to be missed, if the SomeDate value falls outside the selected time. For example, if I want to take all records of TODAY, then I technically want everything between 00:00:00 (midnight) and 23:59:59.
What I would really like to be able to do is use TRUNC around the query variable, as follows:
WHERE
A.SomeDate BETWEEN TRUNC(#variable('Start')) AND TRUNC(#variable('End Date'))
... however this causes a compilation error: "inconsistent datatypes: expected DATE got NUMBER". I don't know why Oracle would treat a prompt as a number datatype before it has compiled.
Does anyone know how I can take the #variable value and convert it into something that I will be able to truncate to a date value?
I'm therefore trying to figure out a way round this. One thing I had in mind was if I could possibly take the prompt variable and convert it explicitly into a date, using TO_DATE
Edit: it has been pointed out to me that TRUNC will have no effect, as the "12:00:00 AM" is already midnight. Therefore I think I have misunderstood TRUNC. It appears that it truncates it to midnight: whereas I thought it simply removed the time portion of the date altogether, meaning that matches would be returned at any time between 00:00:00 and 23:59:59.
What I really want is: if SomeDate has a time portion of, for example, 11:03 then how do I ensure that this will be included when an End Date prompt only specifies the day?
If you want to match SomeDate values between 00:00:00 on Start and 23:59:59 on End you can either adjust the end date to have that time instead of the default midnight, or use a range instead of between:
WHERE
A.SomeDate >= #variable('Start')
AND
A.SomeDate < #variable('End Date') + 1
The + 1 uses Oracle date arithmetic to give you the day after the variable value, so if the user picked "01/01/2016 12:00:00 AM" for both the start and end dates they would evaluate as 2016-01-01 00:00:00 and 2016-01-02 00:00:00 respectively. You can use the interval syntax if you prefer.
By using less-than for the upper limit you get all records where SomeDate is greater than or equal to the start date 2016-01-01 00:00:00 and less than the adjusted end date 2016-01-02 00:00:00 - which is the same as saying up to 2016-01-01 23:59:59. (Or if you has a timestamp column which has sub-second precision, up to 23:59:59.999...).
If the parser assumes the variable will be a string but it is actually a date - causing an 'inconsistent datatypes' error - then you could cast it to a date to satisfy the parser:
WHERE
A.SomeDate >= CAST(#variable('Start') AS DATE)
AND
A.SomeDate < CAST(#variable('End Date') AS DATE) + 1
or if it is actually passed as a string in the format you showed you can explicitly convert it:
WHERE
A.SomeDate >= TO_DATE(#variable('Start'), 'DD/MM/YYYY HH:MI:SS AM')
AND
A.SomeDate < TO_DATE(#variable('End Date'), 'DD/MM/YYYY HH:MI:SS AM') + 1
... making sure you have the correct format; from your example it could be DD/MM/YYYY or MM/DD/YYYY.
Try using TO_CHAR() and TO_DATE() together :
WHERE
A.SomeDate > TO_DATE(TO_CHAR(#variable('Prompt1'),'ddmmyyyy'),'ddmmyyyy')
First off, your problem is not coming from a time value in the prompt value, but rather the time value in SomeDate. Getting rid of that (making the date equal to midnight) will resolve the issue.
Your best bet, if you have the option of modifying the universe, is to create another object. I'm assuming you have an object named SomeDate whose SQL is a.somedate. Create another object, let's call it SomeDateOnly with a definition of trunc(a.somedate)* **.
Since SomeDateOnly will always be a midnight value, you can use Equal To with your prompts, which will produce SQL like:
trunc(a.somedate) = #variable('Prompt1')
which, when rendered by WebI, will produce:
trunc(a.somedate) = '16-08-2016 00:00:00'
This will return all records with a.somedate between 8/16/2016 at 00:00:00 and 8/16/2016 23:59:59.
Of course, you can use BETWEEN to select a range of dates:
trunc(a.somedate) between #variable('Start Date') and #variable('End Date')
Even if you don't have access to the universe, you can still use the above syntax by modifying WebI's generated SQL. (I'm assuming that's what you've been doing, anyway).
If the above works for you, then the following is irrelevant, but I wanted to address it anyway:
The reason for the "invalid number" error you were receiving is because of the way WebI formats dates for SQL. If you have this string in your query:
A.SomeDate = TRUNC(#variable('Prompt1'))
then WebI will replace the #variable(...) with a date string, and render it as the following before sending it to Oracle:
A.SomeDate = TRUNC('16-08-2016 00:00:00')
This, of course, makes no sense to the TRUNC() function as there's nothing to tell it that it's actually a date value.
You could to_date the prompt first, but you have to use the correct date format. WebI sets the nls_date_format for each session to a non-default format, so you would have to use:
A.SomeDate = TRUNC(to_date(#variable('Prompt1')),'dd-mm-yyyy hh24:mi:ss')
But again, this is irrelevant since you need to trunc somedate, not the prompt response values.
*Better still, rename SomeDate to SomeDateTime, and name the new object SomeDate
**This is pretty common - having multiple objects for the same source field. Sometimes you want the date/time value (for listing specific transactions), but sometimes you just need the date (for counting transactions by date). So having both available is very useful.

NSDate compare is false? [duplicate]

This question already has answers here:
NSDate isEqualToDate: not working - does it look at seconds and split seconds?
(3 answers)
Closed 8 years ago.
i compare 2 NSDates which are the same and i get false result.
i cant show how i get this dates because its too long , but i can show what i do :
NSLog(#"this date is:%# , and date we check to equality is:%#",thisDate,dateToFind);
if([thisDate isEqualToDate:dateToFind] )
{
NSLog(#"equal date!"); // not printed!
}
the NSLog show me this :
this date is:2012-09-13 14:23:54 +0000 , and date we check to equality is:2012-09-13 14:23:54 +0000
he doesnt print the NSLog .
why ?
As a few have said it seems to be the fractions of a second that are giving you trouble. The reason for this is that an NSDate is simply an object wrapper around an NSTimeInterval(double) with a value in seconds since the reference date(12AM January 1 2001 GMT).
There are a couple main ways to deal with this. Either check the date to see if it is in a given range, or (more likely based on your question) truncate the fractions of a second completely off.
Truncating seconds from an NSDate is trivial code. You may want to truncate all of the dates that you are storing as you store them for quick comparisons. You can truncate an existing NSDate like this:
NSDate *truncatedDate = [NSDate dateWithTimeIntervalSinceReferenceDate:((NSTimeInterval)lround(originalDateObject.timeIntervalSinceReferenceDate))];
This code is pretty self explanatory. It grabs the date's backing time interval rounds it to an integer casts that back to a time interval and creates a new truncated date.
Once you do this to both dates you can then compare your two truncated dates and they will behave as expected.
Or if you must do something without changing your date data you could simply do:
if (lround(thisDate.timeIntervalSinceReferenceDate) == lround(dateToFind.timeIntervalSinceReferenceDate)){
// If whole seconds are equal, as shown in log, this will execute.
}
NSLog(#"this date is:%# , and date we check to equality is:%#",
thisDate,dateToFind);
Try changing the above with:
NSLog(#"this date is:%f , and date we check to equality is:%f",
[thisDate timeIntervalSince1970],
[dateToFind timeIntervalSince1970]);

Oracle Date format - Strange behaviour

I am writing a SQL query to retrieve data from a table between two dates. I give two inputs as shown. I convert the Date TO_CHAR(DD/MON/YYYY).
1. StartDate > 01/SEP/2009
EndDate < 01/OCT/2009
2. StartDate > 01/SEP/2009
EndDate < 1/OCT/2009
I don't get any result for the first input. When I change it to second one, I get the result.
What is the difference between 01/OCT/2009 & 1/OCT/2009 ?
when comparing dates you should always convert explicitely both sides of the operator to DATE datatype. Assuming StartDate and EndDate are both DATE datatype, this would work:
StartDate > to_date('01/SEP/2009', 'dd/MON/yyyy')
AND EndDate < to_date('01/OCT/2009', 'dd/MON/yyyy')
In your case however the result you get is consistent with VARCHAR comparison. You're comparing strings and it's VERY unlikely you will get the correct result (since months are NOT sorted alphabetically for instance).
If StartDate and EndDate are indeed VARCHAR you should convert them explicitely:
to_date(StartDate, 'dd/MON/yyyy') > to_date('01/SEP/2009', 'dd/MON/yyyy')
AND to_date(EndDate, 'dd/MON/yyyy') < to_date('01/OCT/2009', 'dd/MON/yyyy')
This is valid for all versions of Oracle.
Since I don't have an Oracle client available to verify my answer, it's not certain but: In Oracle, a single digit in the date string representing the day (D) is used to specify the day of week (Monday-Sunday), while 2 digits are used to specify the day of month (3 digits will represent the day in the year). Although you mentioned you're converting the string using what seems like the right way to do the interpretation, I think this could be the source of the problem (or at least could be an explanation to the difference..).